KABUL — Afghan officials announced Sunday that President Ashraf Ghani had won reelection by a hair's breadth in a nationwide September vote, netting 50.64 percent of the preliminary tally — the majority he needed to avoid a second round of voting.

Abdullah Abdullah, Ghani's estranged governing partner and main contender, denounced the preliminary results as "fraudulent" and vowed to challenge them.

The announcement Sunday came after weeks of delay, controversy and protests by opponents.

Six hours after the announcement, the president gave a triumphal televised speech to supporters in his palace, pledging that his new administration would ensure equal rights for all Afghans, build a strong state, and bring Afghanistan "to light from ambiguity" and "to unity from division" after 40 years of conflict and turmoil.

Ghani, 70, gave only a brief nod to the process of resolving electoral complaints, which must take place over the next two weeks before the election results are deemed final. His tone and message in a 30-minute address flanked by his three running mates were those of a confident victor bursting with plans for the future.

Abdullah, who finished second with 39.52 percent, said in several Facebook posts that the results had "no legitimacy" and he would "defend the clean votes" of his supporters "at any cost" rather than allow a group of "rootless fraudsters to rule their fate."

Under the election rules, the leader of the Sept. 28 vote needed 50 percent plus one vote to avoid a runoff. Candidates have 72 hours to file protests with the Independent Electoral Complaints Commission, which then has two weeks to review them.

The repeatedly postponed polls and the troubled vote count that followed sparked nationwide tension. In the days following the election, both Ghani and Abdullah claimed victory. Abdullah’s supporters have held protests in the capital and several provinces, demanding that several hundred thousand questionable ballots be recounted — but then boycotting the actual recount.

The election turnout was low, with fewer than 2 million ballots cast by an electorate of more than 9 million registered voters, suggesting that whoever won would preside over a weak government. But Ghani, who was forced to share power with Abdullah after a fraud-plagued election in 2015, has asserted that even a close victory would give him a strong mandate to spearhead peace talks with Taliban insurgents.

Hawa Alam Nuristani, the head of the election commission, thanked Afghans for their patience while her panel struggled to hold “transparent and fair elections” while coping with a lack of money and staff, technical complications and threats by insurgents. Election officials used biometric technology for the first time to count votes, and there were significant discrepancies between the number of paper ballots and the biometric data results.

In a solemn televised event, Nuristani read out the results for 13 candidates, who polled a total of 1.824 million votes. She said Ghani received 934,868 votes and Abdullah 720,099. Gulbuddin Hekmatyar, a former Islamist militia leader, received 70,243 votes; Rahmatullah Nabil, a former national intelligence chief, received 33,292.

U.S. Ambassador John Bass tweeted that “it’s important for all Afghans to remember that these results are preliminary. Many steps remain before final results are certified, to ensure the Afghan people have confidence.”

The U.N. special representative for Afghanistan, Tadamichi Yamamoto, issued a similar caution: “All Afghan authorities and actors must demonstrate their commitment to safeguard and complete the election.” He said candidates should raise their concerns through the formal process, and officials must deal with them “transparently and thoroughly” to ratify the election “in a credible manner.”

Opponents of Ghani, including influential independents such as Nabil and powerful ethnic leaders backing Abdullah, have claimed Ghani and his aides rigged the election by pressuring poll workers, stuffing ballot boxes and other means. Some of Abdullah’s backers have threatened to provoke a political rebellion if Ghani is found to have cheated to win.

“If he wins cleanly, we will support the result 100 percent, but we will not allow him to impose another fraudulent result,” said Hafiz Mansour, a senior legislator in Jamiat-i-Islami, the largest party supporting Abdullah. If necessary, he said, “we will take over 23 provinces. We understand the consequences, but what alternative is there?”

Others called for calm.

“This country and its people cannot afford more crisis. We have to be patient and wait for the final outcome,” independent lawmaker Gul Ahmad Azimi said on ToloNews TV. “In the history of Afghanistan, no one has accepted defeat. We have to accept each other.”

If enough complaints are validated by election officials, analysts said, Ghani’s total could drop below the threshold at which a runoff would be required.

Many Afghans said that the election should not have been held at all and that reaching a settlement with the Taliban was a far higher priority. The insurgents have been battling Afghan and NATO forces for years, leaving tens of thousands dead. Months of U.S-Afghan negotiations have failed to produce an agreement.

One reason for the low turnout was that many Afghans doubted it would take place. Others were afraid to vote amid Taliban threats of violence. At one Ghani campaign rally, a suicide bomb killed at least 24 people and wounded scores.

Yousuf Rasheed, executive director of the Free and Fair Election forum for Afghanistan, said that even if protests do not erupt, the electorate has become so disillusioned that it might be difficult to restore people’s faith in the democratic process.

“There is a great lack of public confidence in the political environment,” Rasheed said Sunday. “If the opponents start putting on pressure and more protests now, it will not be helpful or improve the process. It will be a disaster.”