Archaeological excavations in and around Madrid have unearthed animal remains dating back half a million years. And there's evidence of human settlements here as far back as 2,000 years BC.
But this area appears not to have had any great significance in the Roman Empire, despite the best efforts of historians to uncover proof to the contrary. The Spanish capital can trace its origins to the 9th century AD when the Moorish leader Mohamed I, Emir of Cordoba, ordered the construction of a military fortress on the bank of the Manzanares River.
The fortress was called Magerit meaning "place of abundant water" because of the number of rivers and streams supplying the area. Magerit, later to become Madrid, was one of a number of Arab strongholds stretching from Andalucia in the south (El Andalus as it was called by the Arabs) to the Christian kingdoms of the north. A section of the original fortified wall still remains today.
The Moors and Christians battled over Magerit until King Alfonso IV wrested control from the Arabs in 1085.
The city flourished under the reign of Carlos I after he succeeded to both the throne of Spain and of the mighty Hapsburg Empire, which incorporated territories stretching from Austria to Holland and from Spain to the American colonies.
But it wasn't until 1561 that Madrid finally took centre state as the official royal seat of the Hapsburgs under Felipe II.
In the wake of Christopher Columbus' journey to America, untold wealth poured into Spain enabling the royals and aristocracy to create sumptuous palaces, churches and mansions while the majority of the populace lived in squalor. Some of Madrid's finest renaissance and baroque buildings are from this period.
The Hapsburgs fizzled out in 1700 when Carlos II died without an heir, heralding the dawn of the new Bourbon era. Under the Bourbon monarchs many more extravagant baroque buildings and monuments were erected and some impressive engineering projects were undertaken including the walling off of the Manzanares River and the beautiful "Puente de Toledo" bridge.
The early years of the 19th century saw Spain's crushing defeat at the hands of the British in the Battle of Trafalgar, the loss of her American colonies and the French invasion which resulted in the wholesale destruction of church property under the orders of Joseph Bonaparte. The Church was seen to represent a serious threat to France and it's believed that around 1600 religious buildings were destroyed in Madrid in the first half of the 19th century.
The growth of the middle classes in the second half of the century saw the city regain some of its former urban splendour with new prosperous areas developing beyond the original old town.
But social and political unrest put Madrid at the centre of a series of military coups which led to the first but short lived republic being established in 1875. The Bourbons were restored just two years later.
Major developments in the early part of the 20th century included the electrification of the tramlines, the creation of the Gran Via and the introduction of the city's Metro.
But a doubling of the population from half a million to nearly one million between 1900 and 1931 led to chronic housing shortages, hardship and ever-louder demands for an end to the monarchy.
The second republic was proclaimed in 1930 but continuing political instability led to Franco's coup d'etat six years later and the infamous thee-year civil war during which Madrid was under almost constant siege. The streets became battlegrounds and the city's historic monuments were bricked up and sandbagged to protect them from missile attacks.
Madrid's reconstruction after the war and subsequent urban development led to the destruction of many palatial mansions and fine buildings to make way for modern, high rise blocks to accommodate a new age of business and commerce.