Madrid´s appearance begins in the ninth century, when Cordoban emir Abderraman II built a fortress here to defend the local population against attacks from Castile and Leon. But Madrid really started on the road to the city we know today when Charles´son, Phillip II, made it the capital of Spain in 1561.
Madrid´s location smack in the middle of the Iberian peninsula certainly played a part in that decision. Perhaps equally important was the lack of non-royal power centers in Madrid -- the Spanish church was headquartered safely down the road in the former capital Toledo, and the city had not built up a class of important local merchants or nobles the way more established cities such as Valladolid or Burgos had.
The city grew rapidly as the seat of government. With no navigable rivers leading to it, and with long and dusty roads between it and other population centers in Spain, the city focused very much on the crown and the court. Not only Spain, but a world empire covering most of the Americans and stretching across broad swaths of the Pacific was administered from this dusty town high on the Castilian plain.