Approximately 1/4–1/2 of short duration gamma-ray bursts (GRBs) are followed by variable X-ray emission lasting ∼100 s with a fluence comparable or exceeding that of the initial burst itself. The long duration and significant energy of this ‘extended emission’ (EE) poses a major challenge to the standard binary neutron star (NS) merger model. Metzger et al. recently proposed that the EE is powered by the spin-down of a strongly magnetized neutron star (a millisecond protomagnetar), which either survives the NS–NS merger or is created by the accretion-induced collapse (AIC) of a white dwarf. However, the effects of surrounding material on the magnetar outflow have not yet been considered. Here we present time-dependent axisymmetric relativistic magnetohydrodynamic simulations of the interaction of the relativistic protomagnetar wind with a surrounding 10−1–10−3 M⊙ envelope, which represents material ejected during the merger, in the supernova following AIC, or via outflows from the initial accretion disc. The collision between the relativistic magnetar wind and the expanding ejecta produces a termination shock and a magnetized nebula inside the ejecta. A strong toroidal magnetic field builds up in the nebula, which drives a bipolar jet out through the ejecta, similar to the magnetar model developed in the case of long-duration GRBs. We quantify the ‘breakout’ time and opening angle of the jet θj as a function of the wind energy flux and ejecta mass Mej. We show that and θj are inversely correlated, such that the beaming-corrected (isotropic) luminosity of the jet (and hence the observed EE) is primarily a function of Mej. Both variability arguments, and the lower limit on the power of magnetar outflows capable of producing bright emission, suggest that the true opening angle of the magnetar jet must be relatively large. The model thus predicts a class of events for which the EE is observable with no associated short GRB. These may appear as long-duration GRBs or X-ray flashes unaccompanied by a bright supernova and not solely associated with massive star formation, which may be detected by future all-sky X-ray survey missions.