Dental enamel is a principal component of teeth1, and has evolved to bear large chewing forces, resist mechanical fatigue and withstand wear over decades2. Functional impairment and loss of dental enamel, caused by developmental defects or tooth decay (caries), affect health and quality of life, with associated costs to society3. Although the past decade has seen progress in our understanding of enamel formation (amelogenesis) and the functional properties of mature enamel, attempts to repair lesions in this material or to synthesize it in vitro have had limited success4-6. This is partly due to the highly hierarchical structure of enamel and additional complexities arising from chemical gradients7-9. Here we show, using atomic-scale quantitative imaging and correlative spectroscopies, that the nanoscale crystallites of hydroxylapatite (Ca5(PO4)3(OH)), which are the fundamental building blocks of enamel, comprise two nanometric layers enriched in magnesium flanking a core rich in sodium, fluoride and carbonate ions; this sandwich core is surrounded by a shell with lower concentration of substitutional defects. A mechanical model based on density functional theory calculations and X-ray diffraction data predicts that residual stresses arise because of the chemical gradients, in agreement with preferential dissolution of the crystallite core in acidic media. Furthermore, stresses may affect the mechanical resilience of enamel. The two additional layers of hierarchy suggest a possible new model for biological control over crystal growth during amelogenesis, and hint at implications for the preservation of biomarkers during tooth development.