  A complete calculation of the pion-nucleon loops that contribute to the
transition operator for $NN\to NN\pi$ up-to-and-including
next-to-next-to-leading order (N$^2$LO) in chiral effective field theory near
threshold is presented. The evaluation is based on the so-called momentum
counting scheme, which takes into account the relatively large momentum of the
initial nucleons inherent in pion-production reactions. We show that the
significant cancellations between the loops found at next-to-leading order
(NLO) in the earlier studies are also operative at N$^2$LO. In particular, the
$1/m_N$ corrections (with $m_N$ being the nucleon mass) to loop diagrams cancel
at N$^2$LO, as do the contributions of the pion loops involving the low-energy
constants $c_i$, i=1...4. In contrast to the NLO calculation however, the
cancellation of loops at N$^2$LO is incomplete, yielding a non-vanishing
contribution to the transition amplitude. Together with the one-pion exchange
tree-level operators, the loop contributions provide the long-range part of the
production operator. Finally, we discuss the phenomenological implications of
these findings. In particular, we find that the amplitudes generated by the
N$^2$LO pion loops yield contributions comparable in size with the most
important phenomenological heavy-meson exchange amplitudes.
