We use the pinch technique formalism to construct the gauge-independent off-shell two-loop fermion self-energy, both for Abelian (QED) and non-Abelian (QCD) gauge theories. The new key observation is that all contributions originating from the longitudinal parts of gauge boson propagators, by virtue of the elementary tree-level Ward identities they trigger, give rise to effective vertices, which do not exist in the original Lagrangian; all such vertices cancel diagrammatically inside physical quantities, such as current correlation functions or S-matrix elements. We present two different, but complementary derivations: First, we explicitly track down the aforementioned cancellations inside two-loop diagrams, resorting to nothing more than basic algebraic manipulations. Second, we present an absorptive derivation, exploiting the unitarity of the S-matrix, and the Ward identities imposed on tree-level and one-loop physical amplitudes by gauge invariance, in the case of QED, or by the underlying Becchi-Rouet-Stora symmetry, in the case of QCD. The propagator-like sub-amplitude defined by means of this latter construction corresponds precisely to the imaginary parts of the effective self-energy obtained in the former case; the real part may be obtained from a (twice subtracted) dispersion relation. As in the one-loop case, the final two-loop fermion self-energy constructed using either method coincides with the conventional fermion self-energy computed in the Feynman gauge.