The Earth's magnetic field is generated by a dynamo in the liquid iron core, which convects in response to cooling of the overlying rocky mantle. The core freezes from the innermost surface outward, growing the solid inner core and releasing light elements that drive compositional convection1,2,3. Mantle convection extracts heat from the core at a rate that has enormous lateral variations4. Here we use geodynamo simulations to show that these variations are transferred to the inner-core boundary and can be large enough to cause heat to flow into the inner core. If this were to occur in the Earth, it would cause localized melting. Melting releases heavy liquid that could form the variable-composition layer suggested by an anomaly in seismic velocity in the 150 kilometres immediately above the inner-core boundary5,6,7. This provides a very simple explanation of the existence of this layer, which otherwise requires additional assumptions such as locking of the inner core to the mantle, translation from its geopotential centre7,8 or convection with temperature equal to the solidus but with composition varying from the outer to the inner core9. The predominantly narrow downwellings associated with freezing and broad upwellings associated with melting mean that the area of melting could be quite large despite the average dominance of freezing necessary to keep the dynamo going. Localized melting and freezing also provides a strong mechanism for creating seismic anomalies in the inner core itself, much stronger than the effects of variations in heat flow so far considered10.