Hungary’s ruling party has suffered a shock defeat in a local byelection, spelling potential challenges ahead for the prime minister, Viktor Orbán, with only weeks to go before a general election.

Orbán, known for his fierce anti-migrant, populist rhetoric, will seek a third consecutive term in an election on 8 April.

Earlier this month, he branded the opposition as out of touch and “hopeless”, but Sunday’s triumph for Péter Márki-Zay in key constituency Hódmezővásárhely may yet prove him wrong.

Marki-Zay, who ran as an independent but enjoyed the backing of the full spectrum of the opposition, won 57.5% of the votes, against 41.5% for ruling party Fidesz candidate Zoltán Hegedűs, with 92% of the votes counted.

Analysts view the battle for Hódmezővásárhely, a small town in southern Hungary, as a bellwether for the national voting mood.

The result was a shock for Fidesz’s supporters, particularly because Hódmezővásárhely is home to key Orbán ally János Lázár, who heads the prime minister’s office. In the last local election in 2014, Fidesz won with 61% of votes.

Voter turnout at over 61% on Sunday appeared to show that supporters of a fragmented opposition could pose more of a challenge to Orbán than previously thought.

Leading news website Index.hu noted not just the opposition candidate’s victory, but also the 15% margin with which he won.

“This victory holds several lessons,” the website said, adding that it will encourage the opposition to work together against Orbán.

“While this victory does not endanger the probable triumph of Fidesz, it will certainly change the mood of the country in the final weeks before the election.”

The Fidesz party has been polling around 50% although an unfolding corruption scandal involving Orbán’s son-in-law, and a row over an alleged government cover-up of refugee intake numbers have provided unexpected bumps for the government in recent weeks.

The size of Fidesz’s likely majority is seen by analysts as the main factor to watch, with a drop from the two thirds majority won four years ago potentially loosening Orbán’s tight grip on power.