That's because of the time dilation near the source of gravitational field.
The energy of the photon observed by an observer at rest is
E = -pU ...... (1)
where p is the momentum of the photon and U is the velocity of the observer in space-time given by
U = (1,0,0,0)/sqrt(-g00) ...... (2)
In this formula, g00 is the 00 component of the metric tensor of the space-time. Inserting E = hv and (2) into (1), we have
v*sqrt(-g00) = -p0/h ...... (3)
Since p0 is conserved in a static gravitational field, we see that v is inversely proportional to sqrt(-g00).
For a simplest case, we make use of the definition of g00 in a static, spherical gravitational field: schwarzschild solution
-g00 = 1 - 2GM/r ...... (4)
Putting (4) into (3):
v*sqrt(1 - 2GM/r) = -p0/h
Therefore, the red-shift of the photon is expected when it is moving away from the gravitational field (i.e., r increases, v decreases).
(Ref: 廣義相對論引論,俞允強著,北京大學出版社,2000)