Saint Marinus left the island of Arba in present-day Croatia with his lifelong friend Leo, and went to the city of Rimini as a stonemason. After the Diocletianic Persecution following his Christian sermons, he escaped to the nearby Monte Titano, where he built a small church and thus founded what is now the city and state of San Marino. The official date of the founding of what is now known as the Republic is 3 September 301.
In 1631, its independence was recognized by the Papacy.
The advance of Napoleon's army in 1797 presented a brief threat to the independence of San Marino, but the country was saved from losing its liberty thanks to one of its Regents, Antonio Onofri, who managed to gain the respect and friendship of Napoleon. Thanks to his intervention, Napoleon, in a letter delivered to Gaspard Monge, scientist and commissary of the French Government for Science and Art, promised to guarantee and protect the independence of the Republic, even offering to extend its territory according to its needs. The offer was declined by the Regents, fearing future retaliation from other states' revanchism.
During the later phase of the Italian unification process in the 19th century, San Marino served as a refuge for many people persecuted because of their support for unification. In recognition of this support, Giuseppe Garibaldi accepted the wish of San Marino not to be incorporated into the new Italian state.
The government of San Marino made United States President Abraham Lincoln an honorary citizen. He wrote in reply, saying that the republic proved that "government founded on republican principles is capable of being so administered as to be secure and enduring."
During World War I, when Italy declared war on Austria-Hungary on 23 May 1915, San Marino remained neutral and Italy adopted a hostile view of Sammarinese neutrality, suspecting that San Marino could harbor Austrian spies who could be given access to its new radiotelegraph station. Italy tried to forcibly establish a detachment of Carabinieri in the republic and then cut the republic's telephone lines when it did not comply. Two groups of ten volunteers joined Italian forces in the fighting on the Italian front, the first as combatants and the second as a medical corps operating a Red Cross field hospital. The existence of this hospital later caused Austria-Hungary to suspend diplomatic relations with San Marino.
From 1923 to 1943, San Marino was under the rule of the Sammarinese Fascist Party (PFS).
During World War II, San Marino remained neutral, although it was wrongly reported in an article from The New York Times that it had declared war on the United Kingdom on 17 September 1940. The Sammarinese government later transmitted a message to the British government stating that they had not declared war on the United Kingdom.
Three days after the fall of Benito Mussolini in Italy, PFS rule collapsed and the new government declared neutrality in the conflict. The Fascists regained power on 1 April 1944 but kept neutrality intact. Despite that, on 26 June 1944 San Marino was bombed by the Royal Air Force, in the belief that San Marino had been overrun by German forces and was being used to amass stores and ammunition. The Sammarinese government declared on the same day that no military installations or equipment were located on its territory, and that no belligerent forces had been allowed to enter. San Marino accepted thousands of civilian refugees when Allied forces went over the Gothic Line. In September 1944, it was briefly occupied by German forces, who were defeated by Allied forces in the Battle of San Marino.
San Marino had the world's first democratically elected communist government - a coalition between the Sammarinese Communist Party and the Sammarinese Socialist Party, which held office between 1945 and 1957.
San Marino is the world's smallest republic, although when Nauru gained independence in 1968 it challenged that claim, Nauru's land mass being only 21 km2 (8.1 sq mi). However Nauru's jurisdiction over its surrounding waters covers 431,000 km2 (166,000 sq mi), an area thousands of times greater than the territory of San Marino.
San Marino became a member of the Council of Europe in 1988 and of the United Nations in 1992. It is neither a member of the European Union, nor of the Eurozone although it uses the euro as its currency.