Miloš Zeman, the Czech Republic’s populist president, has been narrowly re-elected in a tense contest against a liberal challenger, marking a victory for anti-immigrant, far-right forces in the country and potentially tilting its politics in an anti-western direction.

With more than 99.5% of ballots counted in a second-round presidential runoff, Zeman had a decisive lead over Jiří Drahoš, a trained chemist and former head of the Czech academy of sciences, by a thin but decisive margin of 51.5% to 48.5%.

Zeman, who is a vocal supporter of the Russian president, Vladimir Putin, and who has promoted ties with China, has called for referendums on the Czech Republic’s membership of the EU and Nato. Drahoš campaigned to strengthen the country’s ties to both organisations.

The outcome followed a campaign in which Zeman, who has carved out a reputation as a fierce critic of Islam, painted his opponent as weak on immigration, an issue that resonates strongly in the central European country, where opposition to the EU’s migrant quota scheme runs high.

The symbolism of the 73-year-old president’s victory was clear when he made a triumphant speech to supporters in Prague, flanked by Tomio Okamura, leader of the hardline anti-immigrant Freedom and Direct Democracy party (SPD), which supported his re-election campaign.

Okamura, whose party holds a potentially balance-tipping 22 seats in the Czech parliament, last month hosted a far-right conference in the Czech capital attended by Marine Le Pen, leader of the French Front National, Geert Wilders of the Dutch Freedom Party and other European populist politicians.

Zeman’s victory allows him to dictate the shape of a new government headed by Andrej Babiš, a billionaire tycoon who last week resigned as prime minister after his minority government failed to win a parliamentary confidence vote.

The president had vowed to re-appoint Babiš – who faces criminal charges for allegedly obtaining EU funds fraudulently – whatever the election result, but is now likely to use his victory to insist that the next proposed government has majority support in the 200-seat parliament. That could mean pushing Babiš into an alliance with the SPD. Babiš, whose Action of Dissatisfied Citizens (ANO) party has 78 seats, has refused to consider a coalition with the SPD, citing its far-right views. Other parties have declined to enter a coalition with Babiš because of the criminal charges against him.

“Zeman will want to play a controlling role in forming the next government and that’s not a good omen for Babiš,” said Jiri Pehe, a Czech political analyst and director of the New York University in Prague. “The SPD will be Zeman’s card. He owes them and did very well in those areas where it has support.

“I think he could push Babiš into, if not forming a coalition government with them, then at least accepting their support to allow them to stay as a minority government with them, perhaps with the support of the communists.”

Zeman’s victory confounded widespread concerns about his health and the effects of his reported heavy drinking and smoking. The president has turned the latter issue around to cast himself as folksy and authentic. His electoral triumph appeared to be due to strong support in the provinces and far-flung areas, while his opponent scored heavily in the major cities, particularly Prague.