Archaeological research developed in the 1980s, indicates that the early human occupation of Castle hill dates back to prehistoric times, the end of the Bronze Age. Since then there has been no interruption of the occupation of the mouth of the Guadiana River, always on the commercial activity of the Mediterranean sea.
At the time of the Christian Reconquest of the Iberian peninsula, the region was hit by the Portuguese forces in the 1230s. D. Sancho II of Portugal reached the mouth of the Guadiana river, where he won Mértola and Ayamonte (1238).
The conquest of Castro Marim occured after, under the command of the Master of the Order of Santiago, D. Paio Peres Correia (1242). Since then, the crown promoted the repopulation of the Algarve, in charge of the Military Orders. Castro Marim received charters issued by D. Afonso III of Portugal in July 8, 1277, with the determination to rebuild their defense.
Under the reign of King Dinis of Portugal (1279-1325), began the reconstruction of the castle gate, as epigraphic inscription (July 1, 1279).
In the reign of this sovereign, before the extinction of the Order of the Temple, Castro Marim was donated to the newly created Order of Christ who established there its first headquarters from 1319 till 1356. With the transfer of headquarters to Tomar, by order of D. Pedro I of Portugal (1357-1367), the strategic importance of the village fell, beginning to depopulate itself.
The Infante D. Henrique (1395-1460), appointed Master of the Order, resided in this castle.
Later, under the reign of King D. Manuel I (1495-1521), the town received a new charter (August 20, 1504), at which time the king ordered the repair of their defenses, including the castle walls.
With the War of Restoration of Portuguese independence, the defenses of Castro Marim was remodeled, acquiring modern lines with bastions.
In the early twentieth century, the castle of Castro Marim was declared as a National Monument on 16 June 1910.
Only in middle of the 20th century, began the intervention from the government, through the Directorate-General for National Buildings and Monuments (DGEMN), which between 1942 and 1952 developed works of consolidation and reconstruction.
-- Source