Somewhat related to this post.
San Marino was entirely surrounded by the Papal States for a long time. Why did the much stronger Papal States never conquer and annex San Marino?
So let's wind the clock back. Not all the way back to San Marino's supposed founding in the fourth century, but back to the middle of the 1240's.
Nearly Fifty years have passed since the Battle of Legnano, when the united forces of the Lombard cities rout the army of Emperor Frederick "Barbarossa" and his allies. Frederick goes MIA for a few days, turning up at his headquarters in Pavia just when he was about to be presumed dead. he is not in a good position. His cousin, Henry "the Lion" who is Duke of Bavaria, was supposed to show up with reinforcements ages ago, but he's still nowhere to be found. A large number of captured Imperial knights have been clapped in irons are now imprisoned Milan, including Berthold of Zähringen, Duke of Carinzia and a Prince of the Empire.
When his vassals recalled their knights Frederick has no choice but to sue for peace. Negotiations were brokered by a snickering Venetian Doge Sebastiano Ziani (he had subtly backed both sides over the course of the conflict) along with Cardinal Romualdo Guarna, the representative of the King of Sicily. In the deal (signed in Venice 1177, and reconfirmed at Constance in 1183) Frederick confirmed that the Pope had sole power to appoint Bishops, while the Italian cities had the power to appoint their own councils.
But there is was another, historically overlooked series of provisions in the Peace of Venice, which is of little interest for general historians but of great interest to San Marino.
In the treaty, the papacy had made a point to guarantee the independence of the city of Ancona, an important trading hub on the Adriatic coast and a rival which Venice was very much trying to squash. Ancona was one of the many who threw off the shackles of feudal rule and governed itself as an early (oligarchic) republic. As with all the Italian states, "City-State" is a bit of a misnomer, as the city's ruling council of elders ("Consiglio degli Anziani") ruled an area much larger than just the city of Ancona. During Barbarossa's fourth descent in Italy an army under Imperial Legate Christian of Mainz had laid siege to Ancona, which had thrown in its lot with Pope Alexander and the Italian cities. But Ancona resisted the siege even as Christian was able to convince the Venetians to blockade the city's port. By guaranteeing the continued independence of Ancona at war's end, the Papacy gained an important friend in central Italy, a check on Venice, and more importantly was able to leverage a deal with the Council of Elders whereby the city's territory would be ruled by a Papal Legate in the nearby town of Fermo in exchange for protection and non-interference in the city's affairs.
The agreement with Ancona is the most important of many territorial gains the Papacy obtained. In the more sparsely populated central Italy, Frederick had propped up an obscure puppet of his as Duke of Spoleto (Conrad of Urslingen). After peace was signed, sovereignty of these lands was transferred to the Papacy. This is where we go from there being a "Papal State" (one single sovereign state ruled by the Pope) to "Papal States" (multiple states ruled by the Pope). The new papal acquisitions were hodgepodge of independent cities, multi-level feudal holdings, cities within feudal holdings, and cities run by citizens with feudal holdings (fun!).
My point is, the twelfth century papal acquisitions could be seen as thousands of San Marinos of varying size and power. Modern San Marino is a vestige of this.
Over the next century, Papal policy would center on consolidating control over these lands, be it through legal scheming, opportunism, or even force of arms. In the late 13th century, the Papal legate in Fermo was by some machinations able to appoint a Rector in Ancona. San Marino, a small Republican community in the foothills of the Apennines at the time little different from many other communes in north-central Italy, conveniently produced an "ancient" document attesting to the town's ancient independence when the local Abbot came to claim the church's shares of taxes. San Marino was able to keep the local Papal legate out of local affairs. It also helped that the town is virtually unassailable making forcible tax collection difficult.
To small to really bother anyone, San Marino continued governing itself like many other Italian cities, but it's unclear how "independent" a polity like San Marino could be in the early modern era; for example as the Papacy extended it's power over Sigsmondo Malatesta's domain in the 15th century, San Marino "sided" with the Papacy against their neighbor. In return, the tiny republic was granted an expanded territory. San Marino was unceremoniously included in Cesare Borgia's Vassal state in Romagna, and in the 1700's Papal representatives tried (and nearly succeeded) in dismantling the republican institutions. However, at the end of each failed occupation the Republic's government affirmed more and more sovereign powers, such that by the time Napoleon rolled along, he finally put the issue to rest and affirmed his admiration for the "Model Democracy".