X-ray observations of protostellar jets show evidence of strong shocks heating the plasma up to temperatures of a few million degrees. In some cases, the shocked features appear to be stationary. They are interpreted as shock diamonds. We aim at investigating the physics that guides the formation of X-ray emitting stationary shocks in protostellar jets, the role of the magnetic field in determining the location, stability, and detectability in X-rays of these shocks, and the physical properties of the shocked plasma. We performed a set of 2.5-dimensional magnetohydrodynamic numerical simulations modelling supersonic jets ramming into a magnetized medium and explored different configurations of the magnetic field. The model takes into account the most relevant physical effects, namely thermal conduction and radiative losses. We compared the model results with observations, via the emission measure and the X-ray luminosity synthesized from the simulations. Our model explains the formation of X-ray emitting stationary shocks in a natural way. The magnetic field collimates the plasma at the base of the jet and forms there a magnetic nozzle. After an initial transient, the nozzle leads to the formation of a shock diamond at its exit which is stationary over the time covered by the simulations (~ 40 - 60 yr; comparable with time scales of the observations). The shock generates a point-like X-ray source located close to the base of the jet with luminosity comparable with that inferred from X-ray observations of protostellar jets. For the range of parameters explored, the evolution of the post-shock plasma is dominated by the radiative cooling, whereas the thermal conduction slightly affects the structure of the shock.