Aims. We present the first multiwavelength study of Mrk 501 that contains simultaneous very-high-energy (VHE) γ-ray observations and X-ray polarization measurements from the Imaging X-ray Polarimetry Explorer (IXPE). Methods. We used radio-to-VHE data from a multiwavelength campaign carried out between March 1, 2022, and July 19, 2022 (MJD 59639 to MJD 59779). The observations were performed by MAGIC, Fermi-LAT, NuSTAR, Swift (XRT and UVOT), and several other instruments that cover the optical and radio bands to complement the IXPE pointings. We characterized the dynamics of the broadband emission around the X-ray polarization measurements through its multiband fractional variability and correlations, and compared changes observed in the polarization degree to changes seen in the broadband emission using a multi-zone leptonic scenario. Results. During the IXPE pointings, the VHE state is close to the average behavior, with a 0.2–1 TeV flux of 20%–50% of the emission of the Crab Nebula. Additionally, it shows low variability and a hint of correlation between VHE γ-rays and X-rays. Despite the average VHE activity, an extreme X-ray behavior is measured for the first two IXPE pointings, taken in March 2022 (MJD 59646 to 59648 and MJD 59665 to 59667), with a synchrotron peak frequency > 1 keV. For the third IXPE pointing, in July 2022 (MJD 59769 to 59772), the synchrotron peak shifts toward lower energies and the optical/X-ray polarization degrees drop. All three IXPE epochs show an atypically low Compton dominance in the γ-rays. The X-ray polarization is systematically higher than at lower energies, suggesting an energy stratification of the jet. While during the IXPE epochs the polarization angles in the X-ray, optical, and radio bands align well, we find a clear discrepancy in the optical and radio polarization angles in the middle of the campaign. Such results further support the hypothesis of an energy-stratified jet. We modeled broadband spectra taken simultaneous to the IXPE pointings, assuming a compact zone that dominates in the X-rays and the VHE band, and an extended zone stretching farther downstream in the jet that dominates the emission at lower energies. NuSTAR data allow us to precisely constrain the synchrotron peak and therefore the underlying electron distribution. The change between the different states observed in the three IXPE pointings can be explained by a change in the magnetization and/or the emission region size, which directly connects the shift in the synchrotron peak to lower energies with the drop in the polarization degree.