The quantization of the electromagnetic field in lossy and dispersive dielectric media has been widely studied during the last few decades. However, several aspects of energy transfer and its relation to consistently defining position-dependent ladder operators for the electromagnetic field in nonequilibrium conditions have partly escaped the attention. In this work we define the position-dependent ladder operators and an effective local photon-number operator that are consistent with the canonical commutation relations and use these concepts to describe the energy transfer and thermal balance in layered geometries. This approach results in a position-dependent photon-number concept that is simple and consistent with classical energy conservation arguments. The operators are formed by first calculating the vector potential operator using Green's function formalism and Langevin noise source operators related to the medium and its temperature, and then defining the corresponding position-dependent annihilation operator that is required to satisfy the canonical commutation relations in arbitrary geometry. Our results suggest that the effective photon number associated with the electric field is generally position dependent and enables a straightforward method to calculate the energy transfer rate between the field and the local medium. In particular, our results predict that the effective photon number in a vacuum cavity formed between two lossy material layers can oscillate as a function of the position suggesting that also the local field temperature oscillates. These oscillations are expected to be directly observable using relatively straightforward experimental setups in which the field-matter interaction is dominated by the coupling to the electric field. The approach also gives further insight on separating the photon ladder operators into the conventional right and left propagating parts and on the anomalies reported for the commutation relations of the corresponding operators within optical cavities.