Geological evidence shows that ancient Mars had large volumes of liquid water. Models of past hydrogen escape to space, calibrated with observations of the current escape rate, cannot explain the present-day D/H isotope ratio. We simulate volcanic degassing, atmospheric escape, and crustal hydration on Mars, incorporating observational constraints from spacecraft, rovers and meteorites. We find ancient water volumes equivalent to a 100- to 1500-meter global layer are simultaneously compatible with the geological evidence, loss rate estimates, and D/H measurements. In our model, the volume of water participating in the hydrological cycle decreased by 40 to 95% over the Noachian period (~3.7 to 4.1 billion years ago), reaching present-day values by ~3.0 billion years ago. Between 30 and 99% of Martian water was sequestered by crustal hydration, demonstrating that irreversible chemical weathering can increase the aridity of terrestrial planets.