Emperor Charles V left a final codicil in his will for the establishment of a religious foundation in which he was to be buried beside his wife, Isabella of Portugal. harles V’s son, Philip II, undertook the task after his return from Flanders in 1559 and appointed Juan Bautista de Toledo as the royal architect.

In 1560 a site was chosen in a farming area at the foot of the Guadarrama Mountains, 2 km from the town of El Escorial, and entrusted to the Hieronymite Order in the following year. The new monastery, which received its foundation charter in 1567, was named San Lorenzo el Real del Escorial, after St Lawrence of Rome, a martyr of supposed Hispanic origin, and was intended to serve as the royal pantheon.

Tradition, however, also holds that it was dedicated to San Lorenzo de la Victoria, in fulfillment of a vow made by Philip II to atone for the destruction of a church dedicated to St Lawrence at the Battle of San Quintín in 1557.

The first stone was laid on 23 April 1563, and the complex was officially completed on 13 September 1584.

The present town of San Lorenzo del Escorial gradually developed around the royal residence.

A fire at the monastery in 1671 led to the construction of new roofs, which were built by Gaspar de la Peña and Bartolomé Zumbigo y Salcedo, and there was further damage from fire in 1731, 1763 and 1825.

The Hieronymite Order was expelled three times (in 1808, 1837 and 1854) and finally replaced in 1885 by the Augustinians, who now occupy the monastery, although the building is under the administration of the Patrimonio Nacional.

In 1939 many paintings were transferred to the Museo del Prado in Madrid.

Substantial restoration work was carried out from 1953 under the direction of architect Ramón Andrada.

In 1963 a new gallery (Nuevos Museos) was established in the royal palace for the art collection, together with an architectural museum showing material relating to the construction of the building.