According to tradition, Marinus left the island of Rab in Croatia with his lifelong friend Leo and went to the town of Rimini as a stone worker. After persecution because of 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 the state of San Marino. The official date of foundation of the Republic is 3 September 301.
By the mid-5th century, a community was formed; because of its relatively inaccessible location and its poverty, it has succeeded, with a few brief interruptions, in maintaining its independence. In 1631 its independence was recognised by the papacy.
Bust of Giuseppe Garibaldi in San Marino, the first monument in the world dedicated to the "Hero of the Two Worlds". The work of Stefano Galletti, it was erected in 1882.
During the early phase of the Italian unification process of the 19th century, San Marino served as a haven of refuge for numerous persons who were persecuted because of their support for the unification. In memory of this support, Giuseppe Garibaldi accepted the wish of San Marino not to be incorporated into the new Italian state. Napoleon III refused to take the country. He commented, "Why? It's a model republic!"
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."
In World War I, Italy declared war on Austria-Hungary on 23 May 1915. San Marino declared war on Austria-Hungary on 3 June 1915. 
In World War II, San Marino did not join Italy in declaring war on the United Kingdom in 1940. San Marino immediately declared its neutrality. Later, Axis forces retreated through the country and were pursued by the American and British forces. The Allied troops left the country a few weeks later.
The head of state is a committee (council) of two captains-regent. San Marino also holds the record for the world's first democratically-elected communist government, which held office between 1945 and 1957.
San Marino was the world's smallest republic from 301 to 1968, until Nauru gained independence.
San Marino became a member of the Council of Europe in 1988 and of the United Nations in 1992. It is not a member of the European Union.