GENEVA — Voters in Switzerland narrowly backed a proposal to limit immigration Sunday, in a blow for the government after it had warned that the measure could harm the Swiss economy and relations with the European Union.

The decision follows a successful last-minute campaign by nationalist groups that stoked fears of overpopulation and rising numbers of Muslims in the Alpine nation.

Opinion polls before the vote put opponents of the plan in the lead, but as ballot day neared the gap began to close.

Swiss public television SRF reported that some 50.3 percent of voters eventually backed the proposal to introduce quotas for all types of immigrants. About 49.7 percent voted against it, a difference of fewer than 30,000 votes. Support was particularly strong in rural areas, while cities such as Basel, Geneva and Zurich rejected the proposal.

“This has far-reaching consequences for Switzerland … and our relations with the European Union,” Justice Minister Simonetta Sommaruga said after the vote. “It’s a shift away from the current system of free movement of people.”

Although Switzerland isn’t a member of the EU it enjoys close ties to the 28-nation bloc. Bern has painstakingly negotiated a series of bilateral agreements with Brussels, including one that allowed most of the EU’s 500 million citizens to live and work in Switzerland with little formality. Swiss citizens, in turn, could do the same in the European Union.

Under Swiss law the government now has to renegotiate its treaty on the free movement of people, though it is still unclear what kind of caps will be placed on immigration, and when. Switzerland already introduced quotas for immigrants from eight central and eastern European nations two years ago.

The latest decision is likely to have much more far-reaching consequences though, as hundreds of thousands of well-educated foreigners from Germany, France, Italy and other EU countries work in Switzerland. Ahead of Sunday’s referendum business groups warned that many of the 80,000 people who moved to Switzerland last year are vital for the country’s economy.