International audienceThe release of spin-down energy by a magnetar is a promising scenario to power several classes of extreme explosive transients. However, it lacks a firm basis because magnetar formation still represents a theoretical challenge. Using the first three-dimensional simulations of a convective dynamo based on a protoneutron star interior model, we demonstrate that the required dipolar magnetic field can be consistently generated for sufficiently fast rotation rates. The dynamo instability saturates in the magnetostrophic regime with the magnetic energy exceeding the kinetic energy by a factor of up to 10. Our results are compatible with the observational constraints on galactic magnetar field strength and provide strong the...