  We report new near ultraviolet HST/STIS observations of atmospheric
absorptions during the planetary transit of HD209458b. We detect absorption in
atomic magnesium (MgI), while no signal has been detected in the lines of
singly ionized magnesium (MgII). We measure the MgI atmospheric absorption to
be 6.2+/-2.9% in the velocity range from -62 to -19 km/s. The detection of
atomic magnesium in the planetary upper atmosphere at a distance of several
planetary radii gives a first view into the transition region between the
thermosphere and the exobase, where atmospheric escape takes place. We estimate
the electronic densities needed to compensate for the photo-ionization by
dielectronic recombination of Mg+ to be in the range of 10^8-10^9 cm^{-3}. Our
finding is in excellent agreement with model predictions at altitudes of
several planetary radii.
  We observe MgI atoms escaping the planet, with a maximum radial velocity (in
the stellar rest frame) of -60 km/s. Because magnesium is much heavier than
hydrogen, the escape of this species confirms previous studies that the
planet's atmosphere is undergoing hydrodynamic escape. We compare our
observations to a numerical model that takes the stellar radiation pressure on
the MgI atoms into account. We find that the MgI atoms must be present at up to
~7.5 planetari radii altitude and estimate an MgI escape rate of ~3x10^7 g/s.
Compared to previous evaluations of the escape rate of HI atoms, this
evaluation is compatible with a magnesium abundance roughly solar. A hint of
absorption, detected at low level of significance, during the post-transit
observations, could be interpreted as a MgI cometary-like tail. If true, the
estimate of the absorption by MgI would be increased to a higher value of about
8.8+/-2.1%.
