San Marino had given a safe haven to key figures in the Italian unification movement, including Garibaldi, and apparently that's why San Marino’s desire for independence was respected. more info The link also talks about the recognition of san marino by the Kingdom of Italy in 1862.
I don't know why they did not want to unify and why the new Italian state wasn't that bothered. But it might because of san marino's remote location in the Apennines.
Support from France seem to have kept san marino safe from the papal states and Austria during the period, so it seems likely this safeguarded their independence from the kingdom of Italy as well.