Infrared radiation emitted from a planet contains information about the chemical composition and vertical temperature profile of its atmosphere. If upper layers are cooler than lower layers, molecular gases will produce absorption features in the planetary thermal spectrum. Conversely, if there is a stratosphere—where temperature increases with altitude—these molecular features will be observed in emission. It has been suggested that stratospheres could form in highly irradiated exoplanets, but the extent to which this occurs is unresolved both theoretically and observationally. A previous claim for the presence of a stratosphere remains open to question, owing to the challenges posed by the highly variable host star and the low spectral resolution of the measurements. Here we report a near-infrared thermal spectrum for the ultrahot gas giant WASP-121b, which has an equilibrium temperature of approximately 2,500 kelvin. Water is resolved in emission, providing a detection of an exoplanet stratosphere at 5σ confidence. These observations imply that a substantial fraction of incident stellar radiation is retained at high altitudes in the atmosphere, possibly by absorbing chemical species such as gaseous vanadium oxide and titanium oxide.