Swiss voters have rejected a proposal to end an accord with the EU allowing the free movement of people.
With all referendum votes counted, nearly 62% said they wanted to keep free movement, while 38% were against.
Switzerland is not a member of the EU but has a series of interdependent treaties with Brussels which allow it to access to Europe's free trade area.
The move to rein in immigration was proposed by the Swiss People's Party (SVP), but opposed by the government.
A similar initiative to introduce quotas on immigrants from the EU to Switzerland narrowly passed in a 2014 referendum, damaging Swiss-EU relations.
Swiss people are given a direct say in their own affairs under the country's system of direct democracy. They are regularly invited to vote on various issues in national or regional referendums.
Supporters of the anti-free movement plan said it would allow Switzerland to control its borders and select only the immigrants it wants.
Opponents argued it would plunge a healthy economy into recession at an uncertain time and deprive hundreds of thousands of Swiss citizens of their freedom to live and work across Europe.
A landlocked country that has observed neutrality for centuries, Switzerland has over time veered between seeking closer engagement with the EU, and preferring a more isolationist course.
Sunday's referendum could have forced the Swiss government to unilaterally void its free movement agreement with the EU by invoking a so-called guillotine clause.
This clause would have impacted other bilateral deals on transport, research and trade with the EU, disrupting economic activity.
What reaction has there been?
The president of the right-wing SVP, Marco Chiesa, conceded that his campaign had struggled to garner enough support for a proposal which was opposed by the government, parliament and tradio unions.
Given that opposition, Mr Chiesa framed the campaign as a "fight between David and Goliath". "But we will continue to fight for the country and take back control of immigration," he said.
Opponents of the proposal said the result was an expression of Swiss support for open, bilateral relations with the EU. They said voters were worried about the economic cost of ending free movement during the coronavirus pandemic.
"The bilateral path is the right one for Switzerland and for the EU," Justice Minister Karin Keller-Sutter told reporters. "The Swiss people have confirmed this path again today".
"Today is a great day for the relations between the European Union and Switzerland," tweeted European Council President Charles Michel. "The Swiss people have spoken & sent a clear message: together we have a great future ahead of us."
A vote to keep a deal seen as reasonable
Analysis by Imogen Foulkes,BBC News Geneva correspondent
Swiss voters have said a convincing "no" to ending free movement of people with the EU.
The People's Party argued that immigration from Europe was to blame for a rising population, and placed an unsustainable burden on Switzerland's public services, and environment.
In the past, the party has done well with anti-immigration campaigns, but not this time. The economic consequences were clear.
Some 60% of Swiss exports go to Europe, for example. Moreover, half a million Swiss live and work in the EU, and 1.4 million EU citizens work in Switzerland, many in the health service.
Ending free movement would have put the entire relationship with Europe at risk.
Brussels has always told the Swiss they cannot cherry pick: no free trade without free movement. Today, Swiss voters have shown they think that deal is reasonable.
What other issues did Switzerland vote on?
A referendum on paternity leave for new fathers was among the other issues on Sunday's ballot.
The initiative was backed by a majority of voters, heralding a major change in Switzerland, a country seen as lagging behind its European neighbours on parental leave provision.
Under the proposal, fathers will for the first time be granted two weeks of paid leave within six months of the birth of a child. They will also be entitled to receive 80% of their salary, up to a ceiling of 196 Swiss francs (£165; $210) per day.
In addition, voters narrowly approved a government plan to buy new fighter jets, and blocked a revision of Switzerland's hunting law which would have made it easier to cull protected species such as wolves.
What are the possible consequences for Brexit?
The Swiss referendum was already being prepared before the UK voted to leave the EU in 2016.
The SVP - the biggest party in Switzerland's parliament - has used similar arguments to Brexiteers about the benefits of having more control over immigration.
But net migration into Switzerland is actually falling at the moment, and there is a sense voters are becoming weary of the party's anti-immigration message.
The strong vote in favour of free movement could strengthen Brussels' hand with London, and be a signal to the UK of just what kinds of compromises might be needed to agree a free trade deal with the EU.
Timeline: Switzerland and the EU
1992: Swiss vote by 50.3% to 49.7% against joining European Economic Area
1992-2002: Switzerland negotiates, then signs first bilateral agreements with EU - they are interdependent, and include free movement of people - backed by a vote in 2000
2005: Swiss vote to join Europe's Schengen open borders treaty and extend free movement to 10 new EU states
2009: Vote to extend freedom of movement to new EU members Romania and Bulgaria
2014: Swiss narrowly back quotas on EU workers