Strontium and Mg in calcite and aragonite are widely used as proxies of temperature in palaeoenvironmental reconstructions. We use X-ray absorption fine structure (XAFS) to examine Sr and Mg substitution in calcite and aragonite. We have measured the K-edge X-ray absorption near-edge structure (XANES) and extended X-ray absorption fine structure (EXAFS) of Mg and Sr-bearing calcite and aragonite, plus the carbonates: strontianite, hydromagnesite, magnesite, dolomite and a suite of calcites with differing amounts of Mg. The Sr substitutes ideally for Ca in aragonite but causes a small (2%) dilation of the site. Strontium substitutes for octahedral Ca in calcite but with a 6.5% dilation and distortion. Magnesium in the calcites studied provides a variable XANES indicating that the Mg structural state in calcite is variable. Refinement of EXAFS gives Mg–O bond distances of ∼2.12 Å, which are much smaller than the Ca–O bond distance of 2.35 Å but consistent with published amounts of relaxation of the calcite structure. The XANES and EXAFS are consistent with a model whereby some calcites contain nanodomains, e.g. of dolomite and/or huntite structures. The variability in the XANES can be explained by domains of different types and/or sizes. Substitution of Mg into aragonite has 9-fold coordination but relatively short bond distances (2.08 Å) demonstrating either: (1) substantial distortion of the site; or (2) that Mg is accommodated in nanodomains of an unknown phase. Variability in the Mg structural state in calcite may be linked to the variety of temperature dependences observed, e.g. in foraminiferal calcite