During the epoch of reionization a large number of photons were produced with frequencies below the hydrogen Lyman limit. After redshifting into the closest resonance, these photons underwent multiple scatterings with atoms. We examine the effect of these scatterings on the temperature of the neutral intergalactic medium (IGM). Continuum photons, emitted between the Ly alpha and Ly gamma frequencies, heat the gas after being redshifted into the H Ly alpha or D Ly gamma resonance. By contrast, photons emitted between the Ly alpha and Lyman limit frequencies produce effective cooling of the gas. Prior to reionization, the equilibrium temperature of similar to 100 K for hydrogen and helium atoms is set by these two competing processes. At the same time, Ly beta resonance photons thermally decouple deuterium from other species, raising its temperature as high as 10(4) K. Our results have important consequences for the cosmic 21 cm background and the entropy floor of the early IGM, which can affect star formation and reionization.