Purpose: To accurately and efficiently implement charged particle transport in a magnetic field in EGSnrc and validate the code for the use in phantom and ion chamber simulations. Methods: The effect of the magnetic field on the particle motion and position is determined using one- and three-point numerical integrations of the Lorentz force on the charged particle and is added to the condensed history calculation performed by the EGSnrc PRESTA-II algorithm. The code is tested with a Fano test adapted for the presence of magnetic fields. The code is compatible with all EGSnrc based applications, including egs++. Ion chamber calculations are compared to experimental measurements and the effect of the code on the efficiency and timing is determined. Results: Agreement with the Fano test's theoretical value is obtained at the 0.1% level for large step-sizes and in magnetic fields as strong as 5 T. The NE2571 dose calculations achieve agreement with the experiment within 0.5% up to 1 T beyond which deviations up to 1.2% are observed. Uniform air gaps of 0.5 and 1 mm and a misalignment of the incoming photon beam with the magnetic field are found to produce variations in the normalized dose on the order of 1%. These findings necessitate a clear definition of all experimental conditions to allow for accurate Monte Carlo simulations. It is found that ion chamber simulation times are increased by only 38%, and a 10 × 10 × 6 cm3 water phantom with (3 mm)3 voxels experiences a 48% increase in simulation time as compared to the default EGSnrc with no magnetic field. Conclusions: The incorporation of the effect of the magnetic fields in EGSnrc provides the capability to calculate high accuracy ion chamber and phantom doses for the use in MRI-radiation systems. Further, the effect of apparently insignificant experimental details is found to be accentuated by the presence of the magnetic field.