KABUL — Afghan President Ashraf Ghani and his chief rival both declared victory after election results were announced Tuesday, nearly five months after Afghans cast their ballots.

“We not only obtained this victory through the steadfastness of the people, but also foiled internal and external conspiracy,” Ghani told hundreds of supporters in a speech carried by state television.

Hours later, his main challenger, Afghan Chief Executive Abdullah Abdullah, addressed his own supporters, declaring: “We announce our victory.” Abdullah dismissed the official results as “illegal” and said, “Today’s announcement is a coup.”

The contested results come at a critical time for Afghan politics. U.S. and Taliban negotiators have announced a conditional peace deal that could be signed by the end of the month. And substantial challenges to the vote may plunge Afghanistan into a political crisis just as the government needs to unify ahead of intra-Afghan peace talks with the Taliban insurgents.

Ghani appeared to acknowledge the importance of unity as the government prepares to negotiate with the Islamist militant group.

“We call on all Afghans to consolidate together the foundations of the republic . . . and join hands for serving the nation,” he told his supporters. “We want peace, lasting peace, which is the aspiration of the nation, and, God willing, this team will bring peace to all of Afghanistan.”

He won with 50.64 percent of the votes, just enough to avoid a second round of voting, Afghanistan’s Independent Election Commission said Tuesday. According to the Afghan constitution, a presidential candidate needs more than 50 percent of the vote to be declared the winner.

The September vote was marred by widespread accusations of fraud, misconduct and mechanical error, requiring time-consuming recounts that delayed the announcement of the final results. Many of those accusations revolved around ballot stuffing and new technology used to verify the identities of voters using biometrics.

Since Afghanistan began holding elections following the 2001 ouster of the Taliban from Kabul after five years of brutal rule, voting has been riddled with fraud. New technology was intended to change that, but it has inadvertently complicated the vote-counting process.

Many Afghans had called for the election to be postponed because of security concerns. But Ghani wanted the vote to go ahead, saying that an elected Afghan leader with a strong mandate would be needed to enter peace talks with the Taliban after the U.S. peace deal.

The September election coincided with a spike in violence; 85 civilians were killed, and 373 were wounded in election-related attacks, according to a United Nations report. But overall, fewer security incidents than feared were reported on election day.

However, record-low turnout could undermine Ghani’s claim to a strong mandate. Of the more than 9 million Afghans registered to vote, only 1.8 million cast ballots. Many said they stayed away from the polls because of fears of violence and fraud

Abdullah had warned he would contest any final election results marred by fraud. In an interview with The Washington Post before the election, he said his supporters would not be willing to “sacrifice” a legitimate victory at the polls.

When preliminary results were announced in December, Abdullah said 300,000 votes were fraudulent and demanded that they be removed from the final count. His request was denied.

Abdullah netted 39.52 percent of the votes, according to the final results announced Tuesday, about 200,000 votes shy of Ghani’s total.

Ghani and Abdullah were also bitter rivals in Afghanistan’s disputed 2014 presidential election. Both men claimed victory and pushed Kabul to the brink of a political crisis. The deadlock ended only after the United States intervened and brokered a power-sharing deal, with Ghani named president and Abdullah chief executive.

“Both Abdullah and Ashraf Ghani are winners,” Zaki Daryabi, a prominent Afghan journalist, posted to Twitter after the election results were announced. “The losers are the people and the republic of Afghanistan.”

George reported from Islamabad, Pakistan. Sharif Hassan in Kabul contributed to this report.