We report the successful development of a novel methodology of energy reconstruction for very high energy gamma rays detected with Imaging Atmospheric Cherenkov Telescopes (IACTs). This methodology, based on the machine learning algorithm Random Forest, and named RF-Erec, has been adjusted for being used with data from the Major Atmospheric Gamma-ray Imaging Cherenkov (MAGIC) stereo telescope system, which is a worldwide leading instrument for gamma-ray astronomy in the energy range from about 20GeV to beyond 100TeV.The RF-Erec has been evaluated using different realistic scenarios with Monte Carlo simulated data and real observations from the Crab Nebula (the standard candle for the VHE gamma-ray community). This new methodology has been validated by the MAGIC software board, and it is implemented and ready-to-use in the MAGIC Analysis and Reconstruction Software (MARS). This new methodology, validated by the MAGIC software board, has been implemented and is ready for use in the MAGIC Analysis and Reconstruction Software (MARS). We demonstrate that, in comparison to the previous energy reconstruction methodology for MAGIC data, which relied on Look-Up-Tables (LUTs- Erec) and has been utilized in over 100 scientific publications over the last decade, RF-Erec significantly enhances the energy reconstruction of gamma rays. This improvement extends the capabilities of the MAGIC telescopes.Specifically, when quantifying the energy resolution with the width of a Gaussian fitted to the error distribution (resolution-σ), the RF-Erec energy resolution-σ is 20% at 100 GeV and 11% above 1 TeV for Zenith distances (Zd) below 35 degrees, while it is 20% at 1TeV and 13% above 10TeV for Zd above 55 degrees. For a wide range of the observable energies, the improvement of energy resolution-σ, compared to LUTs-Erec, reaches roughly a factor of two, and the improvement is even larger for high Zd observations. Differently to many other works in the literature, our evaluation also considers the energy dispersion and the actual energy migration of events, where RF-Erec improves the performance of LUTs-Erec by factors of a few. The manuscript also demonstrates the importance of energy reconstruction methods with a small energy migration in order to prevent the appearance of artificial spectral features. These artifacts are particularly important at the high end of the gamma-ray spectra, where a few extra high-energy photons could have dramatic consequences for studies related to the EBL attenuation, Lorentz invariance violation, or searches for Axion-like-particles.