Magnetars are highly magnetized neutron stars, the formation mechanism of which is unknown. Hot helium-rich stars with spectra dominated by emission lines are known as Wolf-Rayet stars. We observed the binary system HD 45166 using spectropolarimetry and reanalyzed its orbit using archival data. We found that the system contains a Wolf-Rayet star with a mass of 2 solar masses and a magnetic field of 43 kilogauss. Stellar evolution calculations indicate that this component will explode as a supernova, and that its magnetic field is strong enough for the supernova to leave a magnetar remnant. We propose that the magnetized Wolf-Rayet star formed by the merger of two lower-mass helium stars.