Although no human fossil remains have been found, a great variety of tools have been found, especially in the surroundings of Arganda del Rey and Manzanares, which allow proving the existence of human settlements on the river terraces in the place that today occupies the city.
The conquest and colonization by Rome of the Iberian Peninsula, initially carried out as a Roman military maneuver in its long series of wars with Carthage, lasted almost 200 years, from the Second Punic War to 27 BC. n. and. in which they complete the pacification of the north of the territory and divide it into three provinces. The region currently occupied by Madrid would be located in Tarragona.
In the second half of the 9th century, the Emir of Córdoba Muhammad I (852-886) built a fortress on a promontory next to the river with the purpose of guarding the steps of the Sierra de Guadarrama and being the starting point of raids against the kingdoms Northern Christians. Next to the fortress, the town develops towards the south. This town is called Maǧrīţ, which seems to be an Arabization of the Romance name Matrice, “matrix”, alluding to a stream of that name that crossed the primitive city.
Of the various archaeological works carried out in the city from the middle of the 19th century onwards, they have found remains such as: the Arab wall of the Cuesta de la Vega, the watchtower of the Plaza de Oriente and the remains of a water journey from the Plaza of the Cars. In Arab Madrid, Maslama al-Mayriti was born in the 10th century, called «the Andalusian Euclid», a notable astronomer and founder of a mathematical school in Córdoba.
With the fall of the Taifa kingdom of Toledo in the hands of Alfonso VI of León and Castilla, the city was taken by Christian forces in 1085 without resistance, probably by capitulation. The city and its fortress were integrated into the kingdom of Castile as royal territories. The Christians replace the Muslims in the occupation of the central part of the city, leaving the peripheral neighborhoods or suburbs, which in the previous period were inhabited by a Mozarabic community, such as Morería. During the following century, Madrid continued to receive attacks from the new Muslim powers of the peninsula, the Almoravids, who set the city on fire in 1109 and the Almohads, who put it under siege in 1197. The Christian victory of Las Navas de Tolosa definitively moves away the Muslim influence from the center of the peninsula.
From this time come two outstanding religious events that mark the development of the personality of popular Christianity in Madrid: the “discovery” of the image of the Virgen de la Almudena and the “miraculous” life of Isidro Labrador, who would later be canonized. The city prospers and receives the title of town in 1123.  Following the usual repopulation scheme in Castile, Madrid is constituted as a council, head of a community of town and land, the community of town and land of Madrid.
According to Andyeducation, the government of the city falls to all the people of Madrid with the rank of neighbors, meeting in an open council until in 1346, King Alfonso XI implants the regiment, in which only representatives of the local oligarchy, the regidores, govern the city.. In 1152, King Alfonso VII established the limits of the community of town and land, between the Guadarrama and Jarama rivers. In 1188, a representation of Madrid participated for the first time in the Cortes of Castile. In 1202, Alfonso VIII granted him his first municipal jurisdiction, which regulated the functioning of the council, and whose powers were extended in 1222 by Fernando III el Santo.
In 1625, Felipe IV demolishes the city wall, already surpassed and builds what will be the last one near Madrid. This fence, built exclusively for fiscal reasons (port tax) will limit the growth of the city until the 19th century. The tasks of government are centralized in the Alcázar Real, a group of buildings located on the land that will later be occupied by the Royal Palace and the Plaza de Oriente.
In parallel, the area of another palace is increased in the eastern end of the city, beyond the fence. It is about the Buen Retiro Palace, begun to be built by the Catholic Monarchs (who also moved the Jerónimos de Belém monastery, formerly located near the Manzanares, area of the North Station), of which its gardens are preserved, the Kingdom Hall and the Ballroom, the latter known as the Casón del Buen Retiro and used by the Prado Museum.
Enlightenment and Neoclassicism
The change of dynasty would bring important changes for the city. The monarchs of the new dynasty found it as a dark population, with narrow streets, overcrowded, without sewage systems and pestilential. The Bourbons consider the need to equate Madrid with other European capitals. The fire of the Alcázar Real in 1734 (an unfortunate event that causes the disappearance of a third of the royal collection of paintings) led to the construction of the Royal Palace.
Bridges, hospitals, parks, fountains, buildings for scientific use, sewerage ordinances and other actions were promoted by this last monarch, (who receives the popular title of “best mayor of Madrid”), with the collaboration of architects and urban planners from great professional and artistic category: Francesco Sabatini, Ventura Rodríguez Tizón, Juan de Villanueva among others.
The relationship between the “king mayor” and his subjects-neighbors was not always good: several measures of his modernization program were violently contested during the Esquilache mutiny of 1766, although in which more complex causes also converged.
From the 18th to the 20th century
The uprising of the people of Madrid against the French troops on May 2, 1808 marks the beginning of the War of Independence. King José Bonaparte carried out reforms in the capital, with frequent orders to tear down convents to make squares, for which he acquires the nickname of Pepe Plazuelas. The evolution of the war forced him to flee Madrid on two occasions, but the liberation of the city resulted in the destruction of valuable venues, such as the Buen Retiro Palace.
The confiscation meant a drastic change in the real estate property system, in addition to concentrating a large collection of art that will increase the funds of very important cultural institutions: the Prado Museum (created during the reign of Fernando VII in the building planned for the Cabinet of Sciences) and the National Library. It also means the creation in Madrid of the Central University, which will retain the name Complutense since it comes from the physical and legal transfer of the cloister and students from the renowned University of Alcalá to the nearby capital.
In 1860 the fence of Felipe IV was finally demolished and the city was able to grow, in principle in an orderly manner, thanks to the Castro plan and the completion of the expansion. A modern water supply system is established (the Canal de Isabel II) and communication by rail is established that will make Madrid the center of the radial communications network, which also leaves its mark on the urban fabric.
In the first 30 years of the 20th century, Madrid’s population reached almost one million residents. New suburbs such as Ventas, Tetuán or Carmen welcomed the newly arrived proletariat, while the Madrid bourgeoisie settled in the outskirts. These transformations promoted the idea of the Linear City, by Arturo Soria. At the same time, the Gran Vía was opened in order to decongest the old town and the metro was inaugurated in 1919.