BERLIN — Poland's right-wing president, Andrzej Duda, won a second term by a razor-thin margin in results announced Monday, paving the way for Warsaw to continue a political program that human rights advocates and other European leaders have criticized as undermining democracy.

Duda — an ally of President Trump — won 51 percent of the vote in Sunday's runoff election, the country's electoral commission said. Centrist Warsaw Mayor Rafal Trzaskowski trailed with 49 percent.

Turnout above 68 percent, the highest in a presidential election in 25 years, showed how deeply Poles cared about what was seen as a pivotal vote.

Analysts said the election presented a choice between two Polands: one that clashes with much of the rest of Europe on fundamental values and another that aligns with the European project.

Duda ran a campaign especially notable for its anti-LGBT rhetoric. He proposed a constitutional amendment that would bar single-sex couples from adopting children and at one point suggested that efforts to advance gay rights were worse than communism.

Trzaskowski had advocated for LGBT rights in the past but did not make the issue central to his campaign. Instead, he emphasized promises to protect the country's democratic institutions and unite Poles.

The election was also a referendum on the country's nationalist ruling party, which backed Duda's campaign.

The Law and Justice party has drawn rebukes from the European Union and human rights groups over the past five years, particularly for its attempts to force out independent judges. The party would have remained in control of Poland's lower house of parliament regardless of the outcome of the presidential contest. But because the president can sign or veto new laws, a Trzaskowski win would have presented a stumbling block for the party's agenda.

Affirming his nationalist credentials, Duda vowed in a campaign speech last week to strengthen the Polish state, which he said was “built on our inviolable and sacred tradition.”

Analysts say they expect Law and Justice to leverage his win to further its hard-line policies, including an effort to “re-Polandize” the media and bring foreign-owned outlets under Polish control. That’s something Zbigniew Ziobro, the country’s powerful justice minister, advocated in recent weeks, while Duda took aim at foreign media “meddling” in his campaign.

“Having Duda in office again, the temptation will be huge to make use of him and his signature,” said Piotr Buras, head of the Warsaw office of the European Council on Foreign Relations.

Still, Buras said, the close result should be a "warning sign" for the governing party.

Duda is the first to receive more than 10 million votes since 1990, when Nobel Peace Prize laureate Lech Walesa became the country's first democratically elected president. But Trzaskowski's 9.9 million votes are equally stunning and would have seen him win any other presidential election in the past three decades.

"The division is very deep and the polarization has grown stronger," Buras said, adding that it might complicate efforts to govern. "Those who are politically active are really deeply divided into two halves."

Duda came in first during the initial round of voting last month, but he failed to secure the majority he needed to avoid Sunday’s runoff.

An exit poll on Sunday, which showed a result too close to call, painted a picture of a country divided along lines of age and geography. While Trzaskowski comfortably won over voters under 50, according to the exit poll, Duda drew support from the older generation.

Trzaskowski primarily won in the wealthier and more economically developed cities, while Duda prevailed in Poland’s strongly Catholic and poorer countryside, notably in the east. Duda’s campaign had sought to highlight the expansion of social benefits under Law and Justice party rule, which has mainly helped voters in rural areas that are right-wing strongholds.

Duda struck a somewhat conciliatory tone in a victory speech on Sunday, inviting Trzaskowski to the presidential palace.

“Let us talk to each other and not offend others and look with respect at the other Pole, who has dignity and whose opinion must be respected,” Duda said. “If anyone felt offended by my actions, please accept my apologies.”

While the tight race had prompted concerns that there may be wrangling over the outcome, Trzaskowski conceded defeat on Monday afternoon. "May this term really be different," he wrote on Twitter.

Kalan reported from Warsaw.