Although the first fortifications on this hilltop date from the 1st century BC, archaeological excavations have identified a human presence in the Tagus valley as far back as the 8th century BC. The first fortification was, presumably, erected in 48 BC, when Lisbon was classified as a Roman municipality.
The hill was first used by Celtic tribes, then by Phoenicians, followed by Greeks and later the Carthaginians as a defensive outpost that was later expropriated successively by the Romans, the Suebi, the Visigoths, and the Moors. During the 10th century, the fortifications were built by the Moors; these included the walls or Cerca Moura ("Moorish Encirclement").
In the context of the Christian Reconquista, the castle and the city of Lisbon was conquered from the Moorish Almoravid dynasty in 1147 by Afonso Henriques and northern European knights in the Siege of Lisbon during the Second Crusade; this victory was the only notable success of that failed crusade. According to an oft-repeated legend, the knight Martim Moniz, noticing that one of the doors to the castle was open, prevented the Moors from closing it by throwing his own body into the breach, thus allowing Christian soldiers to enter at the cost of his own life. With the taking of the castle Christian forces were able to maintain the defense of Lisbon until the end of the 12th century.
When Lisbon became the capital of the Kingdom of Portugal in 1255, the castle served as the alcáçova, a fortified residence for Afonso III, in his role as governor. It was extensively renovated around 1300 by King Denis, transforming the Moorish alcáçova into the Royal Palace of the Alcáçova.