A dynamical model for the distribution of dark matter and gas in galaxy clusters