A proposal by the right-wing Swiss People's Party for tougher rules to discourage economic migrants was rejected by a tiny majority of less than 3,000 votes.
Referendum vote
Yes: 1,118,213
No: 1,129,967
Turnout: 46.7%
About 2.25 million people turned out for the nationwide referendum, which produced the closest result on record in Switzerland - just 50.1% voted against the plan.
Under the People's Party proposals, refugees arriving at the Swiss border via another safe country would automatically have been refused asylum.
UN High Commissioner for Refugees Ruud Lubbers issued a stinging denunciation ahead of the vote.
If it was passed, he said, Switzerland would end up with the toughest anti-immigrant legislation in Europe.
'Asylum tourists'
Last year, nearly 21,000 refugees came to Switzerland seeking asylum - a figure the Swiss People's Party said was far too high.
It claimed that many migrants were simply trying to gain access to Switzerland's lucrative labour market.
The party's proposals included a clamp down on "asylum tourists", migrants who shop around in Europe until they find the country with the best benefits to offer.
The right-wing group also wanted authorities to refuse to hear the case of anyone they believed had or could have made an asylum application in a neighbouring safe country.
The Swiss Government condemned the proposals as unworkable, warning that Switzerland's neighbours would refuse to take back large numbers of refugees.
It also argued that a yes vote would have endangered Switzerland's much-vaunted humanitarian traditions and heightened its isolation within Europe.
The UN Refugee Agency also slammed the initiative as "extremely worrying", claiming that thousands of refugees with genuine needs could be stranded on minimum assistance without a chance to tell their story.
Former Yugoslavia accounts for the largest number of asylum seekers to Switzerland, followed by Kurds from Turkey.