The largest party in the Swiss parliament, the rightwing, anti-immigration Swiss People’s party (SVP), called the referendum, arguing that the country must be allowed to set its own limit on the number of foreigners coming in to work.

However, the initiative – opposed by government, parliament, unions, employer organisations and all other political parties because it would put Switzerland’s overall relations with the EU in jeopardy – was rejected by 61.7% of voters, final results showed.

Non-nationals account for roughly a quarter of Switzerland’s 8.6 million inhabitants and the SVP argues the country is facing “uncontrolled and excessive immigration” that will drive up unemployment among Swiss nationals, increase housing costs and overwhelm transport and public services.

Opponents said that ending the two-decade-old free movement accord with the surrounding EU, of which Switzerland is not a member, would rob the country of skilled workers but above all endanger the complex but vital network of more than 120 bilateral treaties that the country has signed with the bloc.

Besides allowing EU nationals to work in Switzerland and vice versa, the treaties include agreements on trade, transport and research in force since 2002 which, if free movement was terminated, would also automatically cease to apply under a so-called “guillotine clause”. The Swiss justice minister, Karin Keller-Sutter, said a vote against free movement would have been “worse than Brexit”.

The SVP’s president, Marco Chiesa, said the party was disappointed but would “continue to fight for the country and take back control of immigration”. Paolo Gentiloni, the EU economy commissioner, described the result as “a beautiful Sunday for democracy and Europe”.

Under Switzerland’s system of direct democracy, the referendum could have forced the government to annul the free movement agreement unilaterally if negotiations with Brussels did not produce a deal on ending the accord – an outcome the EU has repeatedly said it would not permit.

The SVP has tried before to limit free movement, narrowly winning a 2014 referendum demanding immigration quotas. To the party’s fury, the initiative was subsequently watered down, promoting a degree of local preference in some sectors but crucially imposing no fixed limits on EU immigration.

The EU has not shifted its stance since that referendum, insisting that any rejection of the principle of free movement by Switzerland would result in the country being excluded from the single market.

Among other issues on the ballot on Sunday, voters backed introducing paid paternity leave in a move seen as a major change for Switzerland, which lags behind much of Europe on the question of parental leave.

The country, which did not grant women the right to vote until 1971 and which first introduced 14 weeks of paid maternity leave in 2005, will now offer new fathers the chance to take two weeks paid leave after the birth of a child on 80% of their salary up to a ceiling of SFr 196 (£165) a day.