Ambient temperature variation always causes serious spectral drifts in NaI(Tl) gamma spectrum due to the temperature sensitivity of NaI(Tl) spectrometer performance. It is crucial to eliminate spectral drifts for the applications of NaI(Tl) gamma spectroscopy. In this paper, an inversion correction algorithm of gamma spectrum based on the function theory of random variables was proposed. By introducing the energy scaling factor, the theoretical deposition energy spectrum on any energy scale in detector was obtained. To validate the algorithm, the actual measured Ra-226 NaI(Tl) gamma spectra with serious spectral drifts were corrected on different energy scales. The results show that the spectral drifts and spectral intensity variation were effectively eliminated on each energy scale. The maximum relative drift of peak position, the maximum relative variation of peak height and that of FWHM were 36.34, 20.92, and 19.19% before correction, respectively, and after correction, they became 0.12, 2.58, and 2.62%, respectively. These results demonstrate the feasibility and practicality of the developed algorithm in this work for applications in enhancing the confidence degree of identifying NaI(Tl) gamma fingerprints of nuclear materials and the analytical accuracy of NaI(Tl) gamma spectra.