Fast energy switching is of fundamental importance to implement motion mitigation techniques in pencil beam scanning proton therapy, allowing efficient irradiation and high patient throughput. However, depending on magnet design, when switching between different energy layers, eddy currents arise in the bending magnets' yoke, damping the speed of the magnetic field change and lengthening the settling time of the magnetic field. In a proton therapy gantry, this can cause a temporary displacement of the beam trajectory and consequently an incorrect beam position in the bending direction, resulting in an unacceptable loss of position precision at isocenter. The precision can be recovered by either increasing the beam off time after an energy change (waiting until the magnetic field is fully settled) or by actively correcting for the misplacement. We studied the transient magnetic field effects at PSI Gantry 2 in order to develop a correction strategy for this beam position misplacement. We used position and proton range sensitive detectors (segmented strip chamber and multilayer ionization chambers respectively) to measure the difference between expected and actual proton beam position and range as a function of time. The detectors are automatically triggered, read out, and analyzed by the treatment control system. We studied the effects due to the magnets on the gantry and those upstream of the gantry separately, in order to identify which elements contribute the most to the beam position instability. We then designed a spot position algorithm to be applied with the gantry scanning magnets, to correct for the displacement observed as a function of time and achieve the PSI Gantry 2 clinical target of 1mm precision at isocenter at all times, even after an energy change. When switching energy layers in a field, we observed an exponentially decaying spot position displacement at isocenter. The effect increases with increasing energy difference between energy layers (ΔE). The initial residuals between expected and measured position are higher than 1mm for most of the clinical cases at Gantry 2 and fall below 1mm within about 1s or more (depending on ΔE). We found no time dependence for the proton range, thus confirming that the displacement is purely due to a beam trajectory displacement resulting from the longer settling time of the magnetic field. A double exponential model, with two time constants and amplitudes depending on ΔE, fits the data and provides an easy model for the correction function. We implemented this correction as a spot position correction, applied by the scanning magnets during field application. After correction, the residuals were below 0.5mm right after the energy change. We developed a spot position correction for PSI Gantry 2 which reduces the beam off time needed in current state-of-the-art gantries to settle the magnetic fields in the bending magnets. Thanks to this correction, the spot position is stable within 100ms of an energy change at Gantry 2. This is low enough to make possible efficient use of motion mitigation techniques.