BUDAPEST — Voters in Budapest and other Hungarian cities handed a stinging defeat on Sunday to the country’s populist leader, Prime Minister Viktor Orban, and his political machine, sending a wave of opposition candidates to victory in local elections.

Overcoming the governing party’s nearly complete domination of the news media and the state, opposition candidates won control in 11 of Hungary’s 23 larger cities, including the capital, Budapest, compared with three in municipal elections five years ago. They also put another dent in what had seemed a few years ago to be the inexorable march in parts of Europe toward Mr. Orban’s “illiberal” and harshly anti-immigrant politics.

Divided and floundering after nine years under Mr. Orban, the opposition candidates had for years struggled to compete in local, national and European Parliament elections. They were not helped by new election laws that favored the governing Fidesz party.

That changed on Sunday in a vote dominated by competing sex tapes, corruption accusations and doomsday threats of migrants. In a departure from past practices, independent candidates and the leading opposition parties agreed to unite behind one candidate in most towns and cities, successfully fighting the divide-and-conquer tactics of Fidesz.