Hungarian Prime Minister Viktor Orbán speaks to journalists at a polling station in Budapest after casting his vote during local elections. | FERENC ISZA/AFP via Getty Images Orbán suffers defeat as opposition wins Budapest mayoral race Liberal beats incumbent backed by Hungary’s ruling party.

BUDAPEST — Hungarian Prime Minister Viktor Orbán suffered his biggest political setback in a decade on Sunday, when an opposition candidate became mayor of Budapest by defeating an incumbent backed by the ruling party.

Gergely Karácsony, a 44-year-old liberal, triumphed over István Tarlós, an Orbán ally, by 50 percent to about 44 percent, according to preliminary results. Tarlós called Karácsony to congratulate him on his victory, a campaign official said.

The opposition's success in Budapest and in local elections elsewhere in the country came after a wide range of parties and grassroots groups — from liberals to greens, socialists to conservatives — agreed to put aside their differences and run joint candidates for mayoral and district posts in an effort to weaken the ruling Fidesz party's hold on power.

In his second spell as prime minister, which began in 2010, Orbán has dominated Hungarian politics, championing the idea of an "illiberal democracy" and pursuing hard-line anti-migration policies. The European Parliament has accused him of putting the EU's core values at risk and he has also clashed with other EU leaders.

Speaking to reporters and supporters at a bar following his win, Karácsony said that Budapest would now be a green and free city and would be brought "back to Europe."

Besides Budapest, the opposition won mayoral posts in a number of other major cities and towns, including Miskolc, Szeged, and Eger.

Standing next to Orbán in front of a crowd of supporters, 71-year-old incumbent Tarlós thanked those who voted for him and the city of Budapest, adding that "there's nothing to say" wishing "lots of luck" to the new mayor.

The prime minister told supporters that this was a "difficult election campaign" and a big political battle as it "should be" in a competitive democracy. But he pointed out that Fidesz candidates won seats in many towns outside of the capital.

Besides Budapest, the opposition won mayoral posts in a number of other major cities and towns, including Miskolc, Szeged, and Eger. Fidesz won mayorships in Debrecen, Győr, and Kecskemét.

"It is apparent that the majority of Hungarians are fed up with Fidesz," said Katalin Cseh, a member of the European Parliament representing the liberal Momentum party.

The results "showed the power of the opposition against anti-European forces," said Cseh, who also campaigned for local opposition candidates. She said Sunday's results constitute a "very significant change in the political landscape of the country."

A senior Fidesz official said: “I think this can be a beginning of a strained relationship between the government and Budapest.”

While Budapest is traditionally a liberal city, the prime minister's opponents have struggled in recent years to inspire voters and overcome internal divisions. The win on Sunday, together with opposition victories in several smaller cities, is likely to embolden the opposition as it prepares to challenge Orbán at the next general election, due in 2022.

Despite a paucity of resources compared with the ruling party and limited access to mass media and advertising, the opposition was able to rely on social media and a grassroots campaign involving intense personal street campaigning.

The overall election campaign was marked by allegations of irregularities, including a police raid on an opposition campaign office, claims that an opposition party's leadership meetings were bugged, reports of distribution of free food to voters along with Fidesz leaflets, and secret tapes of opposition politicians leaked to government-friendly outlets.

On election day, opposition activists accused the ruling party of engaging in vote-buying, as well as bussing Hungarian speakers across the border from Ukraine to vote.

While Fidesz has not publicly responded to the opposition's specific complaints, the party over the years has always insisted that it does not engage in illegal practices.