Low voter turnout has invalidated Hungary's referendum on European Union refugee quotas, even though citizens voted in support of the government's opposition to any future mandatory schemes to relocate asylum-seekers.
The government claimed a "sweeping victory" while analysts said the result was an "embarrassing but not totally catastrophic defeat" for Prime Minister Viktor Orban.
"We can be proud that we are the first and so far only member state of the European Union" to hold such a referendum, Mr Orban told supporters after results were known.
"Hungarians were able to give their direct opinions on the issue of immigration."
Mr Orban said that despite the invalidity of the ballot, he would present a proposal to amend the Constitution reflecting people's intentions.
"The (European) union's proposal is to let the migrants in and distribute them in mandatory fashion among the member states and for Brussels to decide about this distribution," Mr Orban said.
"Hungarians today considered this proposal and they rejected it. Hungarians decided that only us Hungarians can decide whom we want to live with.
"The question was 'Brussels or Budapest' and we decided this issue is exclusively the competence of Budapest."
With 99.25% of the votes counted, more than 3.2 million voters - or 98.3% of those who cast valid ballots - backed the government.
Turnout stood at 43.9%, the National Election Office said. A rate of 50% plus one vote was needed for the referendum to be valid.
About four per cent of the votes were spoiled - twice as many as in any of the other four referenda held since 1997 - driving down the number of valid votes to just below 40%.
The referendum asked: "Do you want the European Union to be able to prescribe the mandatory settlement of non-Hungarian citizens in Hungary even without the consent of Parliament?"
Mr Orban's Fidesz party claimed victory immediately after voting stations closed, with party vice chairman Gergely Gulyas saying it was a "sweeping victory for all those who reject the EU's mandatory, unlimited quotas".
At the same time, analysts said the relentless government campaign against the EU's refugee relocation schemes had oversaturated citizens.
"Orban was able to dominate public discourse with an issue in which the majority was on his side," said Tamas Boros, analyst at Policy Solutions, a political research and consultancy firm.
"But it seems he went too far and overestimated how much people's opinions are transformed into votes.
"Considering there was hardly any counter-campaign, that they spent some 50 million euro (£43 million) and everyone on the right took up the issues wholeheartedly, it's an embarrassing but not totally catastrophic defeat for Orban.
"It is his first national defeat since 2006, the first time in a decade that the prime minister cannot impose his will."