The mean density of a star transited by a planet, brown dwarf or low mass star can be accurately measured from its light curve. This measurement can be combined with other observations to estimate its mass and age by comparison with stellar models. Our aim is to calculate the posterior probability distributions for the mass and age of a star given its density, effective temperature, metallicity and luminosity. We computed a large grid of stellar models that densely sample the appropriate mass and metallicity range. The posterior probability distributions are calculated using a Markov-chain Monte-Carlo method. The method has been validated by comparison to the results of other stellar models and by applying the method to stars in eclipsing binary systems with accurately measured masses and radii. We have explored the sensitivity of our results to the assumed values of the mixing-length parameter, $alpha_{rm MLT}$, and initial helium mass fraction, Y. For a star with a mass of 0.9 solar masses and an age of 4 Gyr our method recovers the mass of the star with a precision of 2% and the age to within 25% based on the density, effective temperature and metallicity predicted by a range of different stellar models. The masses of stars in eclipsing binaries are recovered to within the calculated uncertainties (typically 5%) in about 90% of cases. There is a tendency for the masses to be underestimated by about 0.1 solar masses for some stars with rotation periods P$_{rm rot}< 7$d. Our method makes it straightforward to determine accurately the joint posterior probability distribution for the mass and age of a star eclipsed by a planet or other dark body based on its observed properties and a state-of-the art set of stellar models.