Large dust reservoirs (up to approximately 10 8 M ⊙) have been detected 1–3 in galaxies out to redshift z ≃ 8, when the age of the Universe was only about 600 Myr. Generating substantial amounts of dust within such a short timescale has proven challenging for theories of dust formation 4,5 and has prompted the revision of the modelling of potential sites of dust production 6–8, such as the atmospheres of asymptotic giant branch stars in low-metallicity environments, supernova ejecta and the accelerated growth of grains in the interstellar medium. However, degeneracies between different evolutionary pathways remain when the total dust mass of galaxies is the only available observable. Here we report observations of the 2,175 Å dust attenuation feature, which is well known in the Milky Way and galaxies at z ≲ 3 (refs. 9–11), in the near-infrared spectra of galaxies up to z ≃ 7, corresponding to the first billion years of cosmic time. The relatively short timescale implied for the formation of carbonaceous grains giving rise to this feature 12 suggests a rapid production process, possibly in Wolf–Rayet stars or supernova ejecta.