We study the origin of atomic contrast on Cu(111) and Pt(111) surfaces probed by a non-contact atomic force microscope and scanning tunnelling microscope. First-principles simulations of the interaction between the atoms of the scanning tip and those of the probed surface show a dependence of the resulting contrast on the tip-sample distance and reveal a close relation between contrast changes and relaxation of atomic positions in both the tip and the sample. Contrast reversion around the distance where the short-range attractive atomic force reaches its maximum is predicted for both types of microscopies. We also demonstrate a relation between the maximal attractive force in a F-z atomic force spectroscopy and the chemical identity of the apex atom on the imaging tip.