ABSTRACT We present Herschel Space Observatory Photodetector Array Camera and Spectrometer (PACS) and Spectral and Photometric Imaging Receiver Fourier Transform Spectrometer (SPIRE FTS) spectroscopy of a sample of 20 massive Young Stellar Objects (YSOs) in the Large and Small Magellanic Clouds (LMC and SMC). We analyse the brightest far-infrared (far-IR) emission lines, that diagnose the conditions of the heated gas in the YSO envelope and pinpoint their physical origin. We compare the properties of massive Magellanic and Galactic YSOs. We find that [O i] and [C ii] emission, that originates from the photo-dissociation region associated with the YSOs, is enhanced with respect to the dust continuum in the Magellanic sample. Furthermore the photoelectric heating efficiency is systematically higher for Magellanic YSOs, consistent with reduced grain charge in low metallicity environments. The observed CO emission is likely due to multiple shock components. The gas temperatures, derived from the analysis of CO rotational diagrams, are similar to Galactic estimates. This suggests a common origin to the observed CO excitation, from low-luminosity to massive YSOs, both in the Galaxy and the Magellanic Clouds. Bright far-IR line emission provides a mechanism to cool the YSO environment. We find that, even though [O i], CO, and [C ii] are the main line coolants, there is an indication that CO becomes less important at low metallicity, especially for the SMC sources. This is consistent with a reduction in CO abundance in environments where the dust is warmer due to reduced ultraviolet-shielding. Weak H2O and OH emission is detected, consistent with a modest role in the energy balance of wider massive YSO environments.