Context. Magnesium is an element of significant astrophysical importance, often traced in late-type stars using lines of neutral magnesium, which is expected to be subject to departures from local thermodynamic equilibrium (LTE). The importance of Mg, together with the unique range of spectral features in late-type stars probing different parts of the atom, as well as its relative simplicity from an atomic physics point of view, makes it a prime target and test bed for detailed ab initio non-LTE modelling in stellar atmospheres. Previous non-LTE modelling of spectral line formation has, however, been subject to uncertainties due to lack of accurate data for inelastic collisions with electrons and hydrogen atoms. Aims. In this paper we build and test a Mg model atom for spectral line formation in late-type stars with new or recent inelastic collision data and no associated free parameters. We aim to reduce these uncertainties and thereby improve the accuracy of Mg non-LTE modelling in late-type stars.Methods. For the low-lying states of Mg i, electron collision data were calculated using the R-matrix method. Hydrogen collision data, including charge transfer processes, were taken from recent calculations by some of us. Calculations for collisional broadening by neutral hydrogen were also performed where data were missing. These calculations, together with data from the literature, were used to build a model atom. This model was then employed in the context of standard non-LTE modelling in 1D (including average 3D) model atmospheres in a small set of stellar atmosphere models. First, the modelling was tested by comparisons with observed spectra of benchmark stars with well-known parameters. Second, the spectral line behaviour and uncertainties were explored by extensive experiments in which sets of collisional data were changed or removed. Results. The modelled spectra agree well with observed spectra from benchmark stars, showing much better agreement with line profile shapes than with LTE modelling. The line-to-line scatter in the derived abundances shows some improvements compared to LTE (where the cores of strong lines must often be ignored), particularly when coupled with averaged 3D models. The observed Mg emission features at 7 and 12 μm in the spectra of the Sun and Arcturus, which are sensitive to the collision data, are reasonably well reproduced. Charge transfer with H is generally important as a thermalising mechanism in dwarfs, but less so in giants. Excitation due to collisions with H is found to be quite important in both giants and dwarfs. The R-matrix calculations for electron collisions also lead to significant differences compared to when approximate formulas are employed. The modelling predicts non-LTE abundance corrections ΔA(Mg) NLTE−LTE in dwarfs, both solar metallicity and metal-poor, to be very small (of order 0.01 dex), even smaller than found in previous studies. In giants, corrections vary greatly between lines, but can be as large as 0.4 dex. Conclusions. Our results emphasise ...