Magnetars are neutron stars with ultrastrong magnetic fields, which can be observed in x-rays. Polarization measurements could provide information on their magnetic fields and surface properties. We observed polarized x-rays from the magnetar 4U 0142+61 using the Imaging X-ray Polarimetry Explorer and found a linear polarization degree of 13.5 ± 0.8% averaged over the 2- to 8-kilo-electron volt band. The polarization changes with energy: The degree is 15.0 ± 1.0% at 2 to 4 kilo-electron volts, drops below the instrumental sensitivity ~4 to 5 kilo-electron volts, and rises to 35.2 ± 7.1% at 5.5 to 8 kilo-electron volts. The polarization angle also changes by 90° at ~4 to 5 kilo-electron volts. These results are consistent with a model in which thermal radiation from the magnetar surface is reprocessed by scattering off charged particles in the magnetosphere.