The conservative prime minister of Poland has won a second five-year term, narrowly beating the liberal mayor of Warsaw, according to a near-complete vote count.
The head of the electoral commission, Sylwester Marciniak said that the official results will be announced later but the commission confirmed that after votes from 99.7% of districts had been counted, President Andrzej Duda had won 51.21% of the share.
His opponent Rafal Trzaskowski had fallen behind on 48.97% of the vote, giving President Duda an a lead of nearly half a million votes.
Incumbent President Andrzej Duda flashes a victory sign in Pultusk, Poland, Sunday, July 12 after the electoral commission confirmed he had won 51.21 per cent of the vote after 99.7 per cent of districts had been counted
The very close race reflected the deep cultural divisions in this European Union nation.
The bitterly fought campaign battle focused on issues of culture, and Duda's government, along with state media and the Catholic church, sought to stoke fears of Jews, the LGBT community and Germans.
Duda's Law and Justice party campaigned on traditional values, with policies including huge monthly cash bonuses of 500 zlotys (£100) per child to all families, irrespective of income.
The Polish prime minister also had strong support among older Poles after he and the party lowered the retirement age and introducing a yearly cash bonus called a '13th pension.'
Duda's supporters gave him and the party credit 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.
Duda shakes hands with supporters in Pultusk, Poland, Sunday, July 12, while celebrating his re-election for a second five-year term
But the party has also stoked conflict with the European Union with laws that have given it vast new powers over the top courts and judicial bodies.
The Law and justice 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.
Representing the centrist Civic Platform party, Trzaskowski, a former European Parliament lawmaker who jumped into the race late, had vowed to protect the country's democratic values and unite the divided society, while preserving the popular welfare policies.
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.
Presidential candidate Rafal Trzaskowski flashes a victory sign at the end of the election day in Warsaw, Poland, Sunday, July 12 when the official exit poll showed the result to be too close to call
Trzaskowski, as Warsaw mayor, had signed a tolerance declaration for LGBT people that triggered a nationwide backlash last year. The ruling party has often 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 for gays and lesbians.
Duda's campaign also portrayed Trzaskowski as someone who would sell out Polish families in favour of Jewish interests.
Jaroslaw Kaczynski, the leader of the Law and Justice party, latched onto comments made by Trzaskowski, saying that a discussion needed to be had to compensate the Jews for property seized from them before the Second World War.
Kaczynski said 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 coverage, alleging there had been 'a German attack in these elections.'
The Foreign Ministry last week also 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.