We use 1-kpc resolution cosmological Adaptive Mesh Refinement (AMR) simulations of a Virgo-like galaxy cluster to investigate the effect of feedback from supermassive black holes on the mass distribution of dark matter, gas and stars. We compared three different models: (i) a standard galaxy formation model featuring gas cooling, star formation and supernovae feedback, (ii) a ‘quenching’ model for which star formation is artificially suppressed in massive haloes and finally (iii) the recently proposed active galactic nucleus (AGN) feedback model of Booth and Schaye. Without AGN feedback (even in the quenching case), our simulated cluster suffers from a strong overcooling problem, with a stellar mass fraction significantly above observed values in M87. The baryon distribution is highly concentrated, resulting in a strong adiabatic contraction (AC) of dark matter. With AGN feedback, on the contrary, the stellar mass in the brightest cluster galaxy (BCG) lies below observational estimates and the overcooling problem disappears. The stellar mass of the BCG is seen to increase with increasing mass resolution, suggesting that our stellar masses converge to the correct value from below. The gas and total mass distributions are in better agreement with observations. We also find a slight deficit (˜10 per cent) of baryons at the virial radius, due to the combined effect of AGN-driven convective motions in the inner parts and shock waves in the outer regions, pushing gas to Mpc scales and beyond. This baryon deficit results in a slight adiabatic expansion of the dark matter distribution that can be explained quantitatively by AC theory.