Photon energy is strictly conserved, if you look at it from the correct perspective. If you consider galaxies to be moving through space (as opposed to space itself expanding), then redshift is merely an accumulation of tiny Doppler shifts along a photon's trajectory through space. Doppler shifts do not constitute a loss of energy per se, rather the redshift occurs as the photon loses momentum relative to the observer, or its wavelength physically stretches as each wavecrest takes longer to arrive, as it chases and enters successive reference frames that are receding ever faster away from the emitter.
The cosmological redshift is a function primarily of location -- that is, the proper distance traveled away from the emitter, which in turn determines the changes in its proper velocity (c + H * D) it experiences in each successive reference frame. (H is the then-current Hubble rate and D is proper distance from the emitter). The photon of course always travels at a local (peculiar) speed of c, but its proper velocity relative to the emitter progressively increases with distance due to Hubble's Law. Note that at large cosmic distances, the accumulation of tiny Doppler shifts along the photon's trajectory yields a much different total redshift figure than would a single end-to-end Doppler shift velocity calculation. The accumulation of redshifts along the trajectory is incremental because the photon must adjust its proper velocity (relative to the emitter) to maintain a local speed of c in each successive recession frame it travels through, even though the photon is not absorbed in any of those frames until it reaches the ultimate observer.
The basic concept of cosmological redshift as Doppler shift, and the implications for conservation of energy, are the subject of the cover article of the current issue of Scientific American, authored by Tamara Davis. You can read it at
www.sciam.com. As she points out, even in the recent past many authors have written as if the cosmological redshift somehow violates energy conservation, and they often resort to unhelpful platitudes such as "General Relativity does not require energy to be conserved in an expanding universe". But really it is just a failure to take the Doppler velocity differential of the observer and emitter fully into account. The photon's momentum relative to its emitter (redshift)
decreases with distance; but its proper velocity relative to its emitter
increases with distance, and in exactly the same proportion. Therefore at each location along the photon's trajectory, multiplying momentum and proper velocity together always yields a constant. That constant relates the photon's energy conservation back to the emitter's reference frame. An increase in proper velocity doesn't come as a free lunch; the price paid for it is the loss of momentum observed as redshift.
You also asked about gravitational redshift. From the perspective of the observer, gravitation causes blueshift, not redshift, as the photon is accelerated toward the observer by the sphere of cosmic mass energy defined by the photon's radius from the observer at each instant in time. However, if redshift is treated as Doppler shift in the FRW metric, then gravitational blueshift is already subsumed in the Doppler shift, so it is not a separate factor in the calculation. The effect of gravitation is reflected in the decrease of the Hubble rate over time. As a result of this decrease, the photon's accumulated proper velocity (H * D) increase relative to the emitter is less than it would have been if gravity weren't progressively slowing down the Hubble rate. Since energy conservation is maintained by the Doppler shift formula, the gravitational contribution is already captured in the overall energy conservation.
But the mathematical role of gravity is coordinate-specific. If Schwarzschild coordinates are used instead of FRW coordinates, then gravitational blueshift becomes a discrete element of the redshift calculation, and it is multiplied by an element equivalent to the accumulated Special Relativistic Doppler shift. In Schwarzschild coordinates, all of the cosmic mass in the sphere defined by the emitter's radius from the observer is considered to be concentrated at the observer's location. Therefore clocks run slower at the observer than at the emitter, and the observer sees more wavecrests arriving per beat of slower local clock time (i.e., higher frequency), which is observed as blueshift. The total energy of a system comprised of a Schwarzschild mass and a photon (or any object) freefalling radially toward it is strictly conserved.