The first fortification on the top of the mountain was built by the Romans. In the Middle Ages it was bitterly fought over by the Byzantines, Goths, Franks and Lombards. Between 961 and 963 Berengar II, the last king of the Lombard Kingdom of Italy, was besieged by Otto I of Saxony. Around the middle of the eleventh century the Counts of Montecopiolo came to Montefeltro, the ancient name of San Leo, from which they took the name and title. In the second half of the 14th century the Malatesta were able to capture the fortress, but until the middle of the 15th century, the castle was often retaken by the Montefeltro. In 1441 the young Federico da Montefeltro scaled the walls of the fort. Faced with the new military dangers, he had the fortress rebuilt, entrusting the task to the Sienese engineer Francesco di Giorgio Martini.