During the pre-Roman era, the site was occupied by a castro founded by the Turduli peoples.
By the 12th century, the land was already occupied by Leonese forces. The project was conceived by the Leonese and began in the 13th century, a period that corresponded to intense fortification of the lines of Riba-Côa, an area that was constantly in dispute between the Portuguese and Castilian monarchs. It was in this context that King D. Alfonso VII of León conceded a foral (charter) to Castelo Melhor between 1209 and 1210, simultaneously ordering the construction of the "castle", in order to consolidate the population and military, which would become important in the succeeding centuries.
As a consequence of the Treaty of Alcanices, in 1297, the land was integrated into Portuguese territory. On 12 June 1298, King D. Dinis confirmed the privileges issued during Leonese dominion, affirming the importance of the new space. At the same time, he ordered work on the castle and dotted on the town a new administrative purpose, even as it continued to project a secondary military importance within the military lines of Riba-Côa.
This minor statute was supported by King Denis. In addition to its transformation and later ruins, he limited the reformulation to the "entrance system to the castle, whose gate began to be flanked by two rectangular towers". This solution, by its scenographic and authoritarian symbolism transmitted regal authority in the region. Parallel with the perimeter wall, the generally circular and singular tower were "oriented toward the settlement, protecting the unique flank where access was possible". Denis's confirmation of Alfonso's privileges, which avoided any sudden changes to the population or administrative organization of the town, but added a keep tower.
In the last quarter of the 14th century, the first repairs were completed on the castle, during the reign of King D. Fernando. Between 1383 and 1385, during the dynastic crisis, the castle became the dominion of the Kingdom of Castile. Following the Treaty of Monção, on 29 November 1389, the Portuguese crown obtained the squares of Olivença, Noudar, Mértola, Castelo Mendo and Castelo Melhor, while ceding Salvaterra and Tui.