Madrid was not a town until the 9th century, when Muslim emir Mohammed I ordered to build a fortress on the area now occupied by the Royal Palace and the Manzanares River. Previously to the erection of the germ of Madrid, there were already Roman and Visigothic settlements spread along both sides of the river, but with very different names (Mantua, Miacum, Ursaria). The few inhabitants of this region before the Muslim conquest, threatened by the imminent enemy conquest, decided to hide in the ancient city wall the image of a virgin that they illuminated with small candles.
Three centuries passed and Alfonso VI, a Christian king, conquered the Muslim city of Magerit, that had evolved around the fortress, in 1083. Two years after, he decided to celebrate a procession around the town, accompanied by king Sancho of Aragon, Infant Ferdinand of Navarra and the selfsame Cid Campeador (legend or reality?). The procession started at the church of Saint Mary and its objective was to find the image of the Virgin that had already been hidden in the wall for two centuries.
And the miracle happened. The procession was at the level of current Cuesta de la Vega when the stones of the ancient wall fell down and inside it Christians found the image of the Virgin with two candles illuminating it.
If we conquer Toledo, I promise to search for the image of Saint Mary of the Meadow, until I reach to find it.
Alfonso VI, year 1085.
Since then, the Virgin of the Almudena (thus named after her discovery next to the Muslim wheat storehouse, called Almudín) is the Patron Saint of Madrid. The legend tells us too that it was brought to Spain by apostle Santiago and she was originally called the Virgin of the Town of Madrid.
The original image was destroyed, as many others, during the Civil War. The sculpture currently preserved at the Cathedral of Madrid is of a late Gothic style, possibly made between the 15th and 16th centuries. The sculpture at the outer part of the cathedral is made of white stone and commemorates the place where the supposed miracle happened.