The magnetic-field conditions in astrophysical relativistic jets can be probed by multiwavelength polarimetry, which has been recently extended to X-rays. For example, one can track how the magnetic field changes in the flow of the radiating particles by observing rotations of the electric vector position angle Ψ. Here we report the discovery of a ΨX rotation in the X-ray band in the blazar Markarian 421 at an average flux state. Across the 5 days of Imaging X-ray Polarimetry Explorer observations on 4–6 and 7–9 June 2022, ΨX rotated in total by ≥360°. Over the two respective date ranges, we find constant, within uncertainties, rotation rates (80 ± 9° per day and 91 ± 8° per day) and polarization degrees (ΠX = 10% ± 1%). Simulations of a random walk of the polarization vector indicate that it is unlikely that such rotation(s) are produced by a stochastic process. The X-ray-emitting site does not completely overlap the radio, infrared and optical emission sites, as no similar rotation of Ψ was observed in quasi-simultaneous data at longer wavelengths. We propose that the observed rotation was caused by a helical magnetic structure in the jet, illuminated in the X-rays by a localized shock propagating along this helix. The optically emitting region probably lies in a sheath surrounding an inner spine where the X-ray radiation is released.