All measurements of cosmic star formation must assume an initial distribution of stellar masses - the stellar initial mass function - in order to extrapolate from the star-formation rate measured for typically rare, massive stars (of more than eight solar masses) to the total star-formation rate across the full stellar mass spectrum 1. The shape of the stellar initial mass function in various galaxy populations underpins our understanding of the formation and evolution of galaxies across cosmic time 2. Classical determinations of the stellar initial mass function in local galaxies are traditionally made at ultraviolet, optical and near-infrared wavelengths, which cannot be probed in dust-obscured galaxies 2,3, especially distant starbursts, whose apparent star-formation rates are hundreds to thousands of times higher than in the Milky Way, selected at submillimetre (rest-frame far-infrared) wavelengths 4,5. The 13C/18O isotope abundance ratio in the cold molecular gas - which can be probed via the rotational transitions of the 13CO and C18O isotopologues - is a very sensitive index of the stellar initial mass function, with its determination immune to the pernicious effects of dust. Here we report observations of 13CO and C18O emission for a sample of four dust-enshrouded starbursts at redshifts of approximately two to three, and find unambiguous evidence for a top-heavy stellar initial mass function in all of them. A low 13CO/C18O ratio for all our targets - alongside a well tested, detailed chemical evolution model benchmarked on the Milky Way 6 - implies that there are considerably more massive stars in starburst events than in ordinary star-forming spiral galaxies. This can bring these extraordinary starbursts closer to the 'main sequence' of star-forming galaxies 7, although such main-sequence galaxies may not be immune to changes in initial stellar mass function, depending on their star-formation densities.