In the first observation of Mars with XMM-Newton, on 20-21 November 2003, this planet is clearly detected as an X-ray source. High resolution X-ray spectroscopy with the Reflection Grating Spectrometer (RGS) confirms that the X-ray radiation from Mars is composed of two different components: one due to fluorescent scattering of solar X-rays in its upper atmosphere and the other one due to solar wind charge exchange in its exosphere. Close to Mars, the RGS spectrum is dominated by two pronounced CO2 fluorescence lines at 23.5 Å and 23.7 Å. Fluorescence from N2 at 31.5 Å is also observed. With increasing distance from Mars, these lines fade, while numerous ( 12) emission lines become prominent at the positions expected for de-excitation of highly ionized C, N, O, and Ne atoms, strongly resembling a cometary X-ray spectrum. The He-like O6+ multiplet is resolved and is dominated by the spin-forbidden magnetic dipole transition 2 ^3S1 to 1 ^1S_0, confirming charge exchange as the origin of the emission, while the resonance line 2 ^1P1 to 1 ^1S0 increases in intensity closer to Mars, where the density of the exosphere is higher. The high spectral dispersion and throughput of XMM-Newton / RGS make it possible to produce X-ray images of the Martian exosphere in individual emission lines, free from fluorescent radiation. They show extended emission out to 8 Mars radii, with morphological differences between individual ions and ionization states. This is the first definite detection of charge exchange induced X-ray emission from the exosphere of another planet, providing a direct link to cometary X-ray emission.