Poland's state electoral commission said that conservative President Andrzej Duda has won 51.21% of the vote with almost all votes counted in the country's weekend election.
The nearly complete results, based on a count of votes from 99.97% districts, shows liberal Warsaw Mayor Rafal Trzaskowski just behind with 48.79% of the vote.
The final results could vary somewhat, however.If the result is born out, it would be one of closest elections in Poland's history, reflecting the deep divisions in this European Union nation. Sunday's vote was originally planned for May but was delayed amid bitter political wrangling.
It follows 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.
Duda, who is backed by the ruling right-wing Law and Justice party, campaigned on traditional values and social spending in this mostly Catholic nation as he sought a second five-year term.
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.
Duda's campaign also cast Trzaskowski as someone who would sell out Polish families 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.