  This paper presents the electron and photon energy calibration obtained with
the ATLAS detector using 140 fb$^{-1}$ of LHC proton-proton collision data
recorded at $\sqrt{s}=13$ TeV between 2015 and 2018. Methods for the
measurement of electron and photon energies are outlined, along with the
current knowledge of the passive material in front of the ATLAS electromagnetic
calorimeter. The energy calibration steps are discussed in detail, with
emphasis on the improvements introduced in this paper. The absolute energy
scale is set using a large sample of $Z$-boson decays into electron-positron
pairs, and its residual dependence on the electron energy is used for the first
time to further constrain systematic uncertainties. The achieved calibration
uncertainties are typically 0.05% for electrons from resonant $Z$-boson decays,
0.4% at $E_\text{T}\sim 10$ GeV, and 0.3% at $E_\text{T}\sim 1$ TeV; for
photons at $E_\text{T}\sim 60$ GeV, they are 0.2% on average. This is more than
twice as precise as the previous calibration. The new energy calibration is
validated using $J/\psi \to ee$ and radiative $Z$-boson decays.
