WARSAW — President Andrzej Duda of Poland appeared on Monday to have narrowly won a second term in the closest presidential election since the end of communist rule in 1989, although the results could still be challenged if his opponent challenges the result by claiming voters’ rights were infringed.
If the decision stands, Mr. Duda will be able to continue on the path set by a conservative nationalist government that has fought to control the courts and media, while stoking fear of gay people, the European Union, foreigners and, recently, Jews.
Mr. Duda fended off a fierce challenge from Rafal Trzaskowski, the liberal mayor of Warsaw. The country’s electoral commission said that with 99 percent of the actual vote counted, Mr. Duda had secured 51.21 percent of the vote. Mr. Trzaskowski won 48.79 percent.
Marcin Matczak, a constitutional expert who has been critical of the sweeping changes to the judicial system, said that the courts would get involved if there is a claim of an infringement by either party, and that it would not be hard for either side to find grounds to do so.
Mr. Trzaskowski, he said, could point to obstacles to voting abroad because of the coronavirus pandemic and the use of public television propaganda against him.
“We still do not have full numbers, so the official result cannot be announced,” said Sylwester Marciniak, the head of the Polish Electoral Commission, although he suggested the result would not change.
Sunday’s runoff election was the closest contest in Poland’s post-communist history.
The president’s re-election would ensure that the governing Law and Justice party, which also controls the Parliament, would be able to continue to reshape the nation in ways that critics contend undermine open political debate and the rule of law, and put it at odds with the European Union, which has accused Poland of damaging democratic values and institutions.
Mr. Trzaskowski had cast the election as a fight for the soul of the nation. He promised to end a government that uses state media to promote its views and silence opposing voices, manipulates the courts and uses fear and division to build support.
The mayor, whose campaign rallies were as likely to feature the blue and gold of the European Union flag as the red and white of Poland, said he wanted to live in a country where “an open hand wins against a clenched fist.”
Mr. Duda, however, dismissed concerns about Poland’s illiberal drift as an invention of foreign interests looking to exert control over the nation. He cast himself as a defender of “traditional families” and attacked Mr. Trzaskowski over his support for L.G.B.T. rights — powerful arguments in a staunchly Catholic country, particularly outside its cosmopolitan cities.
The incumbent received a boost recently from President Trump, who met with him at the White House just days before the election and all but endorsed Mr. Duda. “He’s doing a terrific job,” Mr. Trump said. “The people of Poland think the world of him.”
An already bitter campaign turned even uglier in the final days before Sunday’s vote, with Mr. Duda, the Law and Justice party and its supporters in the right-wing media launching a barrage of attacks on Mr. Trzaskowski.
In the pro-government weekly Sieci, the Warsaw mayor was accused of supporting pedophilia. State television, which has been turned into a propaganda machine for the government, suggested that Mr. Trzaskowski would be controlled by Jewish interests in complicated questions related to restitution of property dating from World War II.
Xenophobic arguments are nothing new for Law and Justice, which took power in 2015 on a campaign against accepting migrants, has described itself as defending Christianity against foreign forces, and has tarred the European Union as a threat to national autonomy. But appeals tinged with anti-Semitism, in a country whose Jews were largely wiped out in the Holocaust, were generally off-limits until recently.
Independent news outlets faced escalating attacks during the campaign, with the governing party claiming that Germany and other outside powers were trying to meddle in the nation’s affairs.
“Have you ever heard such homophobia, such anti-Semitism, such attacks on everybody who is brave enough to say ‘We have had enough’?” Mr. Trzaskowski asked supporters on Friday.
“It’s now or never,” he said.
Monika Pronczuk reported from Warsaw, and Marc Santora from London. Anatol Magdziarz contributed reporting from Warsaw.