A magnetar is a special kind of neutron star. Neutron stars form when a star collapses under its own weight. Gravity squeezes the star to the point that only the pressure of neutrons and quarks can prevent it from forming into a black hole. They are incredibly dense. The mass of a star larger than our Sun is squeezed into a volume only 20 kilometers across. When the star collapses, its magnetic field is also compressed, and so neutron stars typically have very strong magnetic fields. Far stronger than that of the Earth.
But some neutron stars have even stronger magnetic fields. Their magnetic fields are a thousand times stronger than regular neutron stars. So powerful that they cannot have been produced by a single star. These neutron stars are known as magnetars. Their immense magnetic fields generate bursts of gamma rays and x-rays.
So how do some neutron stars form such strong magnetic fields? One possibility is that they form from the merger of two stars. Recently a team looked at this idea by creating computer simulations of merging binary stars. Unlike most models, their simulation included the effects of magnetic fields. They found that when two stars merge, they can produce a larger star. Such large main-sequence stars are known as blue stragglers. The simulations showed that the turbulence produced when the two stars merge can create extremely strong magnetic fields. Thus, when the blue straggler eventually dies and collapses, the simulations show it could produce a magnetar.
About 10% of neutron stars are magnetars, and this agrees with the fraction of neutron stars that could form from mergers. So it looks as if merging stars can produce the most powerful magnets in the universe.
Reference: Schneider, Fabian RN, et al. “Stellar mergers as the origin of magnetic massive stars.” Nature 574.7777 (2019): 211-214.