The abundances of volatile elements in the Earth’s mantle have been attributed to the delivery of volatile-rich material after the main phase of accretion1, 2, 3. However, no known meteorites could deliver the volatile elements, such as carbon, nitrogen, hydrogen and sulfur, at the relative abundances observed for the silicate Earth4. Alternatively, Earth could have acquired its volatile inventory during accretion and differentiation, but the fate of volatile elements during core formation is known only for a limited set of conditions4, 5, 6, 7, 8. Here we present constraints from laboratory experiments on the partitioning of carbon and sulfur between metallic cores and silicate mantles under conditions relevant for rocky planetary bodies. We find that carbon remains more siderophile than sulfur over a range of oxygen fugacities; however, our experiments suggest that in reduced or sulfur-rich bodies, carbon is expelled from the segregating core. Combined with previous constraints9, we propose that the ratio of carbon to sulfur in the silicate Earth could have been established by differentiation of a planetary embryo that was then accreted to the proto-Earth. We suggest that the accretion of a Mercury-like (reduced) or a sulfur-rich (oxidized) differentiated body—in which carbon has been preferentially partitioned into the mantle—may explain the Earth’s carbon and sulfur budgets.