Context.Magnetars are highly magnetized neutron stars that can produce a wide diversity of X-ray and soft gamma-ray emissions that are powered by magnetic dissipation. Their magnetic dipole is constrained in the range of 1014–1015G by the measurement of their spin-down. In addition to fast rotation, these strong fields are also invoked to explain extreme stellar explosions, such as hypernovae, which are associated with long gamma-ray bursts and superluminous supernovae. A promising mechanism for explaining magnetar formation is the amplification of the magnetic field by the magnetorotational instability (MRI) in fast-rotating protoneutron stars (PNS). This scenario is supported by recent global incompressible models, which showed that a dipole field with magnetar-like intensity can be generated from small-scale turbulence. However, the impact of important physical ingredients, such as buoyancy and density stratification, on the efficiency of the MRI in generating a dipole field is still unknown.Aims.We assess the impact of the density and entropy profiles on the MRI dynamo in a global model of a fast-rotating PNS. The model focuses on the outer stratified region of the PNS that is stable to convection.Methods.Using the pseudo-spectral code MagIC, we performed 3D Boussinesq and anelastic magnetohydrodynamics simulations in spherical geometry with explicit diffusivities and with differential rotation forced at the outer boundary. The thermodynamic background of the anelastic models was retrieved from the data of 1D core-collapse supernova simulations from the Garching group. We performed a parameter study in which we investigated the influence of different approximations and the effect of the thermal diffusion through the Prandtl number.Results.We obtain a self-sustained turbulent MRI-driven dynamo. This confirms most of our previous incompressible results when they are rescaled for density. The MRI generates a strong turbulent magnetic field and a nondominant equatorial dipole, which represents about 4.3% of the averaged magnetic field strength. Interestingly, an axisymmetric magnetic field at large scales is observed to oscillate with time, which can be described as a mean-fieldαΩ dynamo. By comparing these results with models without buoyancy or density stratification, we find that the key ingredient explaining the appearance of this mean-field behavior is the density gradient. Buoyancy due to the entropy gradient damps turbulence in the equatorial plane, but it has a relatively weak influence in the low Prandtl number regime overall, as expected from neutrino diffusion. However, the buoyancy starts to strongly impact the MRI dynamo for Prandtl numbers close to unity.Conclusions.Our results support the hypothesis that the MRI is able to generate magnetar-like large-scale magnetic fields. The results furthermore predict the presence of aαΩ dynamo in the protoneutron star, which could be important to model in-situ magnetic field amplification in global models of core-collapse supernovae or binary neutron star mergers.