Polish President Andrzej Duda, a conservative who ran a campaign with homophobic and anti-Semitic overtones, narrowly won a second five-year term in a bitterly fought run-off election, defeating the liberal Warsaw mayor, according to a near-complete count of votes.

Mr Duda received 51.21% of Sunday’s vote with 99.97% of the districts reporting, the state electoral commission said.

His opponent, Rafal Trzaskowski, got 48.79%.

Final results, expected later Monday, could vary slightly, but Mr Duda’s lead appeared unassailable.

Mr Trzaskowski thanked his voters on Twitter and congratulated Mr Duda, expressing hope his second term would be “really different” from his first.

Mr Duda’s supporters celebrated what they saw as a clear mandate for him and the right-wing ruling party that backs him, Law and Justice, to continue on a path that has reduced poverty but raised concerns that democracy is under threat.

Critics and human rights groups expressed concerns that Mr Duda’s victory would boost illiberal tendencies not only at home but also within the European Union, which has struggled to halt an erosion of rule of law in Hungary under prime minister Viktor Orban.

Poland Presidential Election
Warsaw Mayor Rafal Trzaskowski congratulated his opponent (Petr David Josek/AP)

Mr Orban posted a picture of himself on Facebook shaking hands with Mr Duda in the Hungarian parliament with “Bravo!” and graphics of a hand showing a “V” for victory and a Polish flag.

Zselyke Csaky, an expert on central Europe with the human rights group Freedom House, said Mr Duda’s victory gives the party “essentially free rein” until parliamentary elections in 2023 “to do away with limits on its power and work towards destroying Poland’s independent institutions, such as the judiciary or the media”.

The close race reflected the deep cultural divisions in this EU nation.

It followed a bitter campaign dominated by issues of culture in which the government, state media and the influential Roman Catholic Church all mobilised in support of Mr Duda and sought to stoke fears of Jews, LGBT people and Germans.

Mr Duda also got an apparent endorsement from US President Donald Trump with a last-minute White House invitation in late June.

Mr Trump praised Mr Duda, saying: “He’s doing a terrific job. The people of Poland think the world of him.”

Mr Duda’s campaign focused on defending traditional family values in the predominantly Catholic nation of 38 million people, and on preserving social spending policies.

The party’s policies include hugely popular monthly cash bonuses of 500 zlotys per child to all families irrespective of income.

Poland Presidential Election
Supporters of presidential candidate Rafal Trzaskowski wait for his speech at the end of election day in Warsaw (Petr David Josek/AP)

They have helped alleviate poverty in rural regions, and given all families more money to spend.

Mr Duda and the party, both in power since 2015, also solidified support among older Poles by lowering the retirement age and introducing a yearly cash bonus called a “13th pension”.

Many credit Law and Justice for making good on promises to reduce the economic inequality that came with the country’s transition from communism to a market economy three decades ago.

There is a strong sense among them that the economic help is restoring a sense of dignity to their lives after many decades of hardship caused by war, communism and the economic dislocations of capitalism.

The party has also stoked conflict with the EU with laws that have given it vast new powers over the top courts and judicial bodies.

Officials in Brussels have repeatedly expressed concerns over the rule of law in both Poland and Hungary, which were for many years hailed as the most successful new democracies to emerge from behind the Iron Curtain.

Poland’s populist politicians have in the past two years frequently used rhetoric discriminating against LGBT people and other minorities, and the party has turned public television into a propaganda tool used during the campaign to praise Mr Duda and cast Mr Trzaskowski in a negative light.

Poland Presidential Election
Jaroslaw Kaczynski, leader of Poland’s right-wing ruling party, left, casts his ballot in the presidential election run-off in Warsaw (AP)

Sunday’s vote was originally planned for May but was delayed by the coronavirus pandemic.

Turnout was very high at 68.1%, close to a record set in 1995, in a sign of the huge stakes for Poles.

Mr Trzaskowski, a former European Parliament lawmaker who jumped into the race late, said he wanted to protect the country’s democratic values and unite the divided society, while preserving the popular welfare policies.

He represented the centrist opposition Civic Platform party, which held power from 2007 to 2015.

It oversaw strong economic growth but is now blamed by many for allowing the gap to grow between the rich and poor.

As the race tightened in recent weeks, Mr Duda turned further to the right in search of votes.

He denounced the LGBT rights movement as an “ideology” worse than communism.

Mr Trzaskowski, as mayor, had signed a tolerance declaration for LGBT people in his city that triggered a nationwide backlash last year.

The ruling party leader, Jaroslaw Kaczynski, denounced LGBT rights as a foreign import that threatens Polish identity.

The EU has denounced the anti-gay rhetoric and some EU officials have called for funding to be denied to communities that declared themselves to be “LGBT free”, mostly a symbolic gesture with no legal meaning but which has triggered fear among gays and lesbians.

Poland Presidential Election
Incumbent president Andrzej Duda addresses supporters in Pultusk, Poland, on polling day (Czarek Sokolowski/AP)

Mr Duda’s campaign also cast Mr Trzaskowski as someone who would sell out Polish interests to Jewish interests, tapping into old anti-Semitic tropes in a country that was home to Europe’s largest Jewish community before it was decimated by Germany in the Holocaust.

Mr Kaczynski seized on Mr Trzaskowski having said in the past that Poland should still be open to discussing Jewish demands to be compensated for pre-Second World War property that was seized by the Germans and later the communists.

He said last week it made one question if Mr Trzaskowski really had a “Polish soul” and a “Polish heart”.

Mr Duda also lashed out at a German correspondent and a partly German-owned tabloid for their campaign coverage, alleging there had been “a German attack in these elections”.

The Foreign Ministry last week summoned Germany’s top diplomat to complain about the coverage, while Germany’s government insisted that it was not seeking to influence the elections or the work of a free media.