This significant part in the following statement is that the photon releases the energy gained by way of converting it into its momentum. This is the way gained energy by the photon can be made net zero ‘0’ at the end of its interaction with the gravitational field of the massive body, but since the photon do have energy (relativistic mass), so it does have electromagnetic field, more than a gravitational field around it, and so its interaction with the massive body is electromagnetic-gravitational interactions.
In electromagnetic - gravitational fields’ interactions, the electromagnetic field of a light photon in transit interacts with the gravitational field of a massive celestial body in its transit path. As they exchange energy through their fields, interactions depend upon their masses. This phenomenon appears similar in the phenomenon of “gravity assist” that uses the relative movement and gravity of a massive body or other astronomical object to alter the path of a smaller, bypassing body, however, in case of electromagnetic - gravitational fields’ interactions, the speed of a light photon is constant, so the speed cannot change.
As a result such light photons gain energy in an arch-like order as it passes said gravitational body. Since the light photon, in transit, cannot increase its natural speed so it bounds to increase its energetic frequency, correspondingly, it decreases its wavelength, rather it increases its momentum too, as per relevant behaviour of the photons as per Max Planck (𝑝 = ℎ/𝜆). The light photon doesn’t store the energy that it’s gaining temporarily; rather it releases the energy gained by way of converting it into its momentum, as the photon continues its journey within the gravitational influence of a massive celestial body.
The Above interactions causes gravitational lensing (not in relativistic view).