The castle is located in a unique landscape due to the proximity of salt and mouth of the Guadiana River. Within the castle fortification are two epigraphic inscriptions: one, the first to record a settlement established by a Portuguese monarch, while the second carving identifies the alterations occurring under the reign of King Denis.
In the vicinity of the fortress there must have existed a castro, dating to the Neolithic period, and successively occupied by Phoenicians, Greeks (854 BC) and Carthaginians (at the end of the 4th century BC). This settlement was finally destroyed in a major cataclysm, before the arrival of the Romans. Following their arrival, the fortification was reconstructed and began occupying an important regional position economically and politically, extending into the Moorish occupation.
Throughout the decade of 1230, the Portuguese Reconquista of the eastern Algarve, by forces of the Order of Santiago, created an influx of settlers into the region of Castro Marim. In 1238, King Sancho II of Portugal captured the mouth of the Guadiana River, which laid the scene for the definitive capture of Castro Marim in 1242, by Paio Peres Correia. Its place along the former Arab-Christian divide meant that settlement by Christians remained weak, resulting in the initiatives in 1274 by King Afonso III of Portugal to populate the settlement of Castro Marim. On 8 July 1277, a foral charter was issued by the King in order to create incentives to settlement, at the same time ordering the reconstruction of the fortress.
During the last quarter of the 13th century, King Denis of Portugal compensating for the loss of Ayamonte to Castile, reinforced the fortifications of Castro Marim, then referred to as Castelo Velho (Old Castle), and then constructed the Castelo de Fora (the barbican) to defend the population of the settlement. On 1 July 1279, the replacement or construction of the castle gate began: it was one of the first public works begun under the reign of King Denis (from an inscription located on the site).