Quench of superconducting radio-frequency cavities frequently leads to the lowered quality factor Q0, which had been attributed to the additional trapped magnetic flux. Here we demonstrate that the origin of this magnetic flux is purely extrinsic to the cavity by showing no extra dissipation (unchanged Q0) after quenching in zero magnetic field, which allows us to rule out intrinsic mechanisms of flux trapping such as generation of thermal currents or trapping of the rf field. We also show the clear relation of dissipation introduced by quenching to the orientation of the applied magnetic field and the possibility to fully recover the quality factor by requenching in the compensated field. We discover that for larger values of the ambient field, the Q-factor degradation may become irreversible by this technique, likely due to the outward flux migration beyond the normal zone opening during quench. Our findings are of special practical importance for accelerators based on low- and medium-beta accelerating structures residing close to focusing magnets, as well as for all high-Q cavity-based accelerators.