Most of the light from blazars, active galactic nuclei with jets of magnetized plasma that point nearly along the line of sight, is produced by high-energy particles, up to around 1 TeV. Although the jets are known to be ultimately powered by a supermassive black hole, how the particles are accelerated to such high energies has been an unanswered question. The process must be related to the magnetic field, which can be probed by observations of the polarization of light from the jets. Measurements of the radio to optical polarization—the only range available until now—probe extended regions of the jet containing particles that left the acceleration site days to years earlier1-3, and hence do not directly explore the acceleration mechanism, as could X-ray measurements. Here we report the detection of X-ray polarization from the blazar Markarian 501 (Mrk 501). We measure an X-ray linear polarization degree ΠX of around 10%, which is a factor of around 2 higher than the value at optical wavelengths, with a polarization angle parallel to the radio jet. This points to a shock front as the source of particle acceleration and also implies that the plasma becomes increasingly turbulent with distance from the shock.