A reply to /u/Manyrandomtacos
Madrid, or to be precise it's nearby ground chosen to be site of the new royal residence complex of El Escorial, was the choice made by Philip II in 1561. As the royal court largely settled in Madrid centered around the Real Alcazar, Philip II also started to build a sophisticated bureaucracy to enable him to rule his empire spanning the entire globe.
Prior to this time, Madrid and Valladolid were two of Philip's residences, however the court was largely itinerant. Compared to Toledo and Valladolid, Madrid had great water supply and large open areas. Plus it is in the geographic center of the peninsula. Philip had shown preference for Madrid as he became king, so he expanded the Real Alcazar. At the same time, he liked to use El Escorial as a place of retreat.
It wasn't until the Spanish victory over the French in St Quentin that he founded a monastery and mausoleum at El Escorial to honor St. Lawrence and celebrate this victory, an expansion from El Escorial's prior use as a palace of remembrance of Charles V.
Unlike his very well-traveled father Charles V (I of Spain), Philip largely stayed in Spain although he did not always stay in the Madrid area. His successors largely followed his style, further strengthening Madrid's position as the capital of the empire.