The origin of lunar magnetic anomalies remains unresolved after their discovery more than four decades ago. A commonly invoked hypothesis is that the Moon might once have possessed a thermally driven core dynamo, but this theory is problematical given the small size of the core and the required surface magnetic field strengths. An alternative hypothesis is that impact events might have amplified ambient fields near the antipodes of the largest basins, but many magnetic anomalies exist that are not associated with basin antipodes. Here we propose a new model for magnetic field generation, in which dynamo action comes from impact-induced changes in the Moon’s rotation rate. Basin-forming impact events are energetic enough to have unlocked the Moon from synchronous rotation, and we demonstrate that the subsequent large-scale fluid flows in the core, excited by the tidal distortion of the core–mantle boundary, could have powered a lunar dynamo. Predicted surface magnetic field strengths are on the order of several microTeslas, consistent with palaeomagnetic measurements5, and the duration of these fields is sufficient to explain the central magnetic anomalies associated with several large impact basins.