The total number of photons may not be constant in the expanding universe.
So even though the energy of an individual photon may decrease due to red shift,
the increase in the number of photons compensates for it and the total energy is conserved.
The following article may be of interest as to how these "post big bang" photons are created :
www.wbabin.net/physics/rothenstein16.pdf