Observations of martian surface morphology have been used to argue that an ancient ocean once existed on Mars. It has been thought that significant quantities of such water could have been supplied to the martian surface through volcanic outgassing, but this suggestion is contradicted by the low magmatic water content that is generally inferred from chemical analyses of igneous martian meteorites. Here, however, we report the distributions of trace elements within pyroxenes of the Shergotty meteorite--a basalt body ejected 175 million years ago from Mars--as well as hydrous and anhydrous crystallization experiments that, together, imply that water contents of pre-eruptive magma on Mars could have been up to 1.8%. We found that in the Shergotty meteorite, the inner cores of pyroxene minerals (which formed at depth in the martian crust) are enriched in soluble trace elements when compared to the outer rims (which crystallized on or near to the martian surface). This implies that water was present in pyroxenes at depth but was largely lost as pyroxenes were carried to the surface during magma ascent. We conclude that ascending magmas possibly delivered significant quantities of water to the martian surface in recent times, reconciling geologic and petrologic constraints on the outgassing history of Mars.