It has long been recognized that Earth and other differentiated planetary bodies are chemically fractionated compared to primitive, chondritic meteorites and, by inference, the primordial disk from which they formed. However, it is not known whether the notable volatile depletions of planetary bodies are a consequence of accretion1or inherited from prior nebular fractionation2. The isotopic compositions of the main constituents of planetary bodies can contribute to this debate3,4,5,6. Here we develop an analytical approach that corrects a major cause of measurement inaccuracy inherent in conventional methods, and show that all differentiated bodies have isotopically heavier magnesium compositions than chondritic meteorites. We argue that possible magnesium isotope fractionation during condensation of the solar nebula, core formation and silicate differentiation cannot explain these observations. However, isotopic fractionation between liquid and vapour, followed by vapour escape during accretionary growth of planetesimals, generates appropriate residual compositions. Our modelling implies that the isotopic compositions of magnesium, silicon and iron, and the relative abundances of the major elements of Earth and other planetary bodies, are a natural consequence of substantial (about 40 per cent by mass) vapour loss from growing planetesimals by this mechanism.