  Magnetic fields are ubiquitous in the Universe. The Sun's magnetic field
drives the solar wind and causes solar flares and other energetic surface
phenomena that profoundly affect space weather here on Earth. The first
magnetic field in a star other than the Sun was detected in 1947 in the
peculiar A-type star 78 Vir. It is now known that the magnetic fields of the
Sun and other low-mass stars (<1.5 solar masses) are generated in-situ by a
dynamo process in their turbulent, convective envelopes. Unlike such stars,
intermediate-mass and high-mass stars (>1.5 solar masses; referred to as
"massive" stars here) have relatively quiet, radiative envelopes where a
solar-like dynamo cannot operate. However, about 10% of them, including 78 Vir,
have strong, large-scale surface magnetic fields whose origin has remained a
major mystery. The massive star $\tau$ Sco is a prominent member of this group
and appears to be surprisingly young compared to other presumably coeval
members of the Upper Scorpius association. Here, we present the first 3D
magneto-hydrodynamical simulations of the coalescence of two massive
main-sequence stars and 1D stellar evolution computations of the subsequent
evolution of the merger product that can explain $\tau$ Sco's magnetic field,
apparent youth and other observed characteristics. We argue that field
amplification in stellar mergers is a general mechanism to form
strongly-magnetised massive stars. These stars are promising progenitors of
those neutron stars that host the strongest magnetic fields in the Universe,
so-called magnetars, and that may give rise to some of the enigmatic fast radio
bursts. Strong magnetic fields affect the explosions of core-collapse
supernovae and, moreover, those magnetic stars that have rapidly-rotating cores
at the end of their lives might provide the right conditions to power
long-duration gamma-ray bursts and super-luminous supernovae.
