Gamma-ray bursts (GRBs) are short-lived transients releasing a large amount of energy (10^51-10^53 erg) in the keV-MeV energy range. GRBs are thought to originate from the internal dissipation of the energy carried by ultra-relativistic jets launched by the remnant of a massive star death or a compact binary coalescence. While thousands of GRBs have been observed over the last thirty years, we still have an incomplete understanding of where and how the radiation is generated in the jet. A novel investigation of the GRB emission mechanism, via time-resolved spectral analysis of the X-ray tails of bright GRB pulses, enables us to discover a unique relation between the spectral index and the flux. This relation is incompatible with the long-standing scenario invoked to interpret the X-ray tails, that is, the delayed arrival of photons from high-latitude parts of the jet. We show that our results provide for the first time evidence of adiabatic cooling and efficient energy exchange between the emitting particles in the relativistic outflow of GRBs.