SUMMARY Inversions of planetary gravity are aimed at constraining the mass distribution within a planet or moon. In many cases, constraints on the interior structure of the planet, such as the depth of density anomalies, must be assumed a priori, to reduce the non-uniqueness inherent in gravity inversions. Here, we propose an alternative approach that embraces the non-uniqueness of gravity inversions and provides a more complete view of related uncertainties. We developed a Transdimensional Hierarchical Bayesian (THB) inversion algorithm that provides an ensemble of mass distribution models compatible with the gravitational field of the body. Using this ensemble of models instead of only one, it is possible to quantify the range of interior parameters that produce a good fit to the gravity acceleration data. To represent the interior structure of the planet or moon, we parametrize mass excess or deficits with point masses. We test this method with synthetic data and, in each test, the algorithm is able to find models that fit the gravity data of the body very well. Three of the target or test models used contain only point mass anomalies. When all the point mass anomalies in the target model produce gravity anomalies of similar magnitudes and the signals from each anomaly are well separated, the algorithm recovers the correct location, number and magnitude of the point mass anomalies. When the gravity acceleration data of a model is produced mostly by a subset of the point mass anomalies in the target model, the algorithm only recovers the dominant anomalies. The fourth target model is composed of spherical caps representing lunar mass concentration (mascons) under major impact basins. The algorithm finds the correct location of the centre of the mascons but fails to find their correct outline or shape. Although the inversion results appear less sharp than the ones obtained by classical inversion methods, our THB algorithm provides an objective way to analyse the interior of planetary bodies that includes epistemic uncertainty.