Even though the pension your receive from Social security is based on the amount you pay in, social security is really an old-age insurance and not a pension fund. If you die before reaching retirement age, the money you have contributed to social security (AHV/AVS) is lost in that it is not paid out to your heirs but simply disappears into the pension scheme.
However, aside from your old-age contributions, you also pay premiums for survivors insurance from social security. This insurance pays out a survivors pension to your dependents if they meet eligibility requirements.
If you are not a Swiss citizen or a citizen of a country which Switzerland has a social security agreement with, you can claim full reimbursement of total social security contributions if you leave Switzerland (assuming you have worked in Switzerland for at least 1 year).