Transit observations in the Mg i line of HD 209458b revealed signatures of neutral magnesium escaping the upper atmosphere of the planet , while no atmospheric absorption was found in the Mg ii doublet . Here we present a 3D particle model of the dynamics of neutral and ionized magnesium populations , coupled with an analytical modeling of the atmosphere below the exobase . Theoretical Mg i absorption line profiles are directly compared with the absorption observed in the blue wing of the line during the planet transit . Observations are well-fitted with an escape rate of neutral magnesium \dot { M } _ { \mathrm { Mg ^ { 0 } } } =2.9 \stackrel { +0.5 } { { } _ { -0.9 } } \times 10 ^ { 7 } g s ^ { -1 } , an exobase close to the Roche lobe ( R _ { \mathrm { exo } } =3 \stackrel { +1.3 } { { } _ { -0.9 } } R _ { \mathrm { p } } , where R _ { \mathrm { p } } is the planet radius ) and a planetary wind velocity at the exobase v _ { \mathrm { pl - wind } } =25 km s ^ { -1 } . The observed velocities of the planet-escaping magnesium up to -60 km s ^ { -1 } are well explained by radiation pressure acceleration , provided that UV-photoionization is compensated for by electron recombination up to \sim 13 R _ { \mathrm { p } } . If the exobase properties are constrained to values given by theoretical models of the deeper atmosphere ( R _ { \mathrm { exo } } =2 R _ { \mathrm { p } } and v _ { \mathrm { pl - wind } } =10 km s ^ { -1 } ) , the best fit to the observations is found at a similar electron density and escape rate within 2 \sigma . In all cases , the mean temperature of the atmosphere below the exobase must be higher than \sim 6100 K. Simulations predict a redward expansion of the absorption profile from the beginning to the end of the transit . The spatial and spectral structure of the extended atmosphere is the result of complex interactions between radiation pressure , planetary gravity , and self-shielding , and can be probed through the analysis of transit absorption profiles in the Mg i line .