Although the Moon currently has no internally generated magnetic field, palaeomagnetic data, combined with radiometric ages of Apollo samples, provide evidence for such a magnetic field from approximately 3.9 to 3.6 billion years (Gyr) ago, possibly owing to an ancient lunar dynamo. But the presence of a lunar dynamo during this time period is difficult to explain, because thermal evolution models for the Moon yield insufficient core heat flux to power a dynamo after approximately 4.2 Gyr ago. Here we show that a transient increase in core heat flux after an overturn of an initially stratified lunar mantle might explain the existence and timing of an early lunar dynamo. Using a three-dimensional spherical convection model, we show that a dense layer, enriched in radioactive elements (a 'thermal blanket'), at the base of the lunar mantle can initially prevent core cooling, thereby inhibiting core convection and magnetic field generation. Subsequent radioactive heating progressively increases the buoyancy of the thermal blanket, ultimately causing it to rise back into the mantle. The removal of the thermal blanket, proposed to explain the eruption of thorium- and titanium-rich lunar mare basalts, plausibly results in a core heat flux sufficient to power a short-lived lunar dynamo.