Photochemistry is a fundamental process of planetary atmospheres that regulates the atmospheric composition and stability1. However, no unambiguous photochemical products have been detected in exoplanet atmospheres so far. Recent observations from the JWST Transiting Exoplanet Community Early Release Science Program2,3 found a spectral absorption feature at 4.05 μm arising from sulfur dioxide (SO2) in the atmosphere of WASP-39b. WASP-39b is a 1.27-Jupiter-radii, Saturn-mass (0.28 MJ) gas giant exoplanet orbiting a Sun-like star with an equilibrium temperature of around 1,100 K (ref. 4). The most plausible way of generating SO2 in such an atmosphere is through photochemical processes5,6. Here we show that the SO2 distribution computed by a suite of photochemical models robustly explains the 4.05-μm spectral feature identified by JWST transmission observations7 with NIRSpec PRISM (2.7σ)8 and G395H (4.5σ)9. SO2 is produced by successive oxidation of sulfur radicals freed when hydrogen sulfide (H2S) is destroyed. The sensitivity of the SO2 feature to the enrichment of the atmosphere by heavy elements (metallicity) suggests that it can be used as a tracer of atmospheric properties, with WASP-39b exhibiting an inferred metallicity of about 10× solar. We further point out that SO2 also shows observable features at ultraviolet and thermal infrared wavelengths not available from the existing observations.