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.
The new structure allowed for a dynamic counter-offensive, providing for cross-fire. The fortress sides were equipped with artillery and the access points were rendered unreachable by enemy fire thanks to military outposts.
In 1502, Cesare Borgia, with the support of Pope Alexander VI, took possession of the fortress. At the death of the pope, in 1503, Guidobaldo da Montefeltro took possession of his dominions. In 1516 the Florentine troops, supported this time by Pope Leo X and guided by Antonio Ricasoli, penetrated the city and commandeered the fort.
From 1527 until the devolution to the Papal State from the Duchy of Urbino in 1631, San Leo belonged to the Della Rovere. With the new ownership, the fortress was used as a prison. Among the inmates were Felice Orsini and Freemason Alessandro Cagliostro. In 1906 the fortress ceased to be a prison and for eight years, until 1914, it hosted a "compagnia di disciplina".
In the period of Italia Unita, the municipality of San Leo belonged to the province of Marche (Province of Pesaro and Urbino), until 15 August 2009 when it was separated together with six other municipalities of the Valmarecchia, following the outcome of a referendum held on 17 and 18 December 2006.
Currently the castle houses a museum and an art gallery in arms.