Unlike Earth, where astronomical climate forcing is comparatively small, Mars experiences dramatic changes in incident sunlight that are capable of redistributing ice on a global scale. The geographic extent of the subsurface ice found poleward of approximately +/-60° latitude on both hemispheres of Mars coincides with the areas where ice is stable. However, the tilt of Mars' rotation axis (obliquity) changed considerably in the past several million years. Earlier work has shown that regions of ice stability, which are defined by temperature and atmospheric humidity, differed in the recent past from today's, and subsurface ice is expected to retreat quickly when unstable. Here I explain how the subsurface ice sheets could have evolved to the state in which we see them today. Simulations of the retreat and growth of ground ice as a result of sublimation loss and recharge reveal forty major ice ages over the past five million years. Today, this gives rise to pore ice at mid-latitudes and a three-layered depth distribution in the high latitudes of, from top to bottom, a dry layer, pore ice, and a massive ice sheet. Combined, these layers provide enough ice to be compatible with existing neutron and gamma-ray measurements.