Upvote:5
Madrid is the capital of Spain because King Philip II of Spain moved the court there. Where the court (and king) go, so does the government (in a monarchy). And he was arguably the most "dominant" Spanish king of "modern" times (Spain's navy declined after the failure of the Armada). No subsequent king of Spain saw fit to overturn Philip II's decision.
Earlier kings had moved the capital from Toledo (too close to Moorish possessions) north to Valladolid for safety. By the time of Philip II, it was safe to move the capital back south, (and Madrid is not far from Toledo). But Philip apparently liked a castle, the Escorial, nearer Madrid.