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

Duda's supporters celebrated what they saw as a clear mandate from voters 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 Duda's victory would boost illiberal tendencies not only at home but also within the EU, which has struggled to halt an erosion of rule of law in Hungary under Prime Minister Viktor Orban.

Orban on Monday posted a picture of himself on Facebook shaking hands with 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 Duda's victory gives the party “essentially free rein” until the next parliamentary elections scheduled 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 state electoral commission said Duda had 51.21% of the vote based on a count of votes from 99.97% districts. His opponent, Rafal Trzaskowski, trailed with 48.79% of the vote.

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

The very close race reflected the deep cultural divisions in this European Union nation.

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

Duda also got an apparent endorsement from U.S. President Donald Trump with a last-minute White House invitation in late June. Trump praised Duda, saying: “He's doing a terrific job. The people of Poland think the world of him.”

Duda's campaign focused on defending traditional family values in the mostly 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 ($125) per child to all families irrespective of income. They have helped alleviate poverty in rural regions, and given all families more money to spend.

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 Duda and cast Trzaskowski in a bad light.

Sunday's vote was originally planned for May but was delayed amid bitter political wrangling.

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 was in power in 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 became tighter in recent weeks, Duda turned further to the right in search of votes. He seized on gay rights as a key theme, denouncing the LGBT rights movement as an “ideology” worse than communism.

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.

Duda's campaign also cast 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.

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

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

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 press.