Using observations of X-ray pulsar Hercules X-1 by the Imaging X-ray Polarimetry Explorer we report a highly significant (>17σ) detection of the polarization signal from an accreting neutron star. The observed degree of linear polarization of ~10% is far below theoretical expectations for this object, and stays low throughout the spin cycle of the pulsar. Both the degree and angle of polarization exhibit variability with the pulse phase, allowing us to measure the pulsar spin position angle 57(2) deg and the magnetic obliquity 12(4) deg, which is an essential step towards detailed modelling of the intrinsic emission of X-ray pulsars. Combining our results with the optical polarimetric data, we find that the spin axis of the neutron star and the angular momentum of the binary orbit are misaligned by at least ~20 deg, which is a strong argument in support of the models explaining the stability of the observed superorbital variability with the precession of the neutron star.