By Patrick McGroarty in Lagos, Nigeria, and Heidi Vogt in Yola, Nigeria 

Nigerians chose a former military dictator and anticorruption crusader to lead Africa's top economy, after a bruising election spotlighted the government's struggle to tackle an Islamist insurgency and to spread its oil wealth more equitably across a deeply divided nation.

With just one state left to report, Nigeria's election commission said Muhammadu Buhari had won 53% of 29 million votes cast over the weekend. President Goodluck Jonathan, the first incumbent to lose an election since democracy returned to the nation in 1999, won 46%.

Mr. Jonathan called Mr. Buhari, a retired general, to congratulate him on his victory, said Osita Chidoka, Nigeria's aviation minister and a close associate of the president. Mr. Chidoka said the president would address the nation later Tuesday.

A peaceful transition would be a historic step for Nigeria and the continent over which it holds increasing sway. The country's fitful democracy has suffered postelection violence in the past, and a presidential race has never been this close.

Human Rights Watch says more than 800 people were killed after Mr. Jonathan defeated Mr. Buhari in 2011. This was Mr. Buhari's fourth run for the presidency.

From the commercial capital of Lagos to towns in the northeast beset by the Islamist insurgency Boko Haram, Nigerians filled the streets on Tuesday to celebrate Mr. Buhari's victory.

"Jonathan didn't see everything we need--so many things," said Joseph Odeh, a 19-year-old assistant cook listening to radio reports of the president's defeat while perched on a pile of cinder blocks in Lagos. "Buhari will bring change to our country."

Both candidates had pledged to respect the results if they deemed them credible. That became a critical caveat this week as both camps complained about a chaotic voting process in some quarters of this country of 170 million, Africa's most populous.

Mr. Jonathan's Peoples Democratic Party said earlier Tuesday that it wanted results thrown out in a number of northern states that backed Mr. Buhari.

The PDP's Peter Godsday Orubebe also accused the commission of investigating reports of voting irregularities only in a southern state Mr. Buhari hoped to win. "We have lost confidence in the process," he said.

Mr. Buhari's party brushed aside the claims. "They are just bad losers," said party spokesman Lai Mohammed. The party urged its supporters to remain calm even if Mr. Jonathan's backers attempt "to subvert the course of the electoral process and, indeed, democracy in the country."

Before the outcome was determined on Tuesday, White House spokesman Josh Earnest praised Nigerians for voting and said it appeared that the election was free and fair, given the presence of international observers.

In 1990, just three African countries were democracies, according to Freedom House, a Washington-based pro-democracy group. By 1994, the count was 18. Now, just 19 qualify.

Angola and Zimbabwe are among countries that have staged what civil groups called deeply flawed elections in recent years, extending the rule of presidents in office for three decades.

Other countries including Senegal and Zambia have managed peaceful transitions after opposition candidates won elections. But an opposition victory against an entrenched ruling party in a country with Nigeria's oil wealth is unprecedented, said Muna Ndulo, a professor at Cornell University Law School.

"Peaceful transfer of power through elections is possible in Africa," he said.

The shakeout for Nigeria's political class, which draws its power from the continent's top crude industry, will be deeply felt, said Stephen Chan, an international-relations professor at London's School of Oriental and African Studies. "Big beasts in the political landscape in Nigeria will be seeing the writing on the wall."

Some warned that Mr. Buhari needs to prove he can live up to the democratic mandate he has received.

"Gen. Buhari would be making a mistake if he felt we will all go to sleep again now," said Ayo Obe, a Nigerian human-rights advocate.

Mr. Buhari's backers say he intends to meet that challenge. He led the country for 20 months after a 1983 military coup, but now says he embraces democratic rights.

"He's a repentant democrat," said Rotimi Amaechi, governor of the oil-rich Rivers State and a campaign leader for Mr. Buhari.

Mr. Buhari will also face a tall order in meeting his promises to crack down on corruption and revive Nigeria's sagging economy. Plunging crude prices have shot big holes in the federal budget, and the naira currency is trading at record lows.

As a northerner with deep ties to the elite business and traditional Muslim leaders in the region, the president-elect is positioned to forge partnerships there that eluded Mr. Jonathan. Prominent northern leaders like Borno State Gov. Kashim Shettima and former central bank chief Sanusi Lamido Sanusi ended up adversaries of Mr. Jonathan's.

Nigerians waited in long lines this week to fill up on gas and withdraw cash from automated-teller machines. Demand for everything from Champagne to small loans has sagged along with the oil price.

"Things are almost at a standstill," said Olutoyin Okeowo, an executive at a group that normally imports 35,000 Toyota vehicles annually. Last year, business fell more than a third as sentiment soured in Africa's top economy. He is counting on a change in government to turn things around.

"Now Nigerians are more politically aware. They know what to expect," Mr. Okeowo said. "The government must live up to that."

In Yola, cars honked as they sped through town and young men shouted and held up Buhari campaign posters. Others burned Jonathan posters.

"We are so happy! We are celebrating!" yelled 23-year-old Aminu Yahya. He said he expected Mr. Buhari to bring employment and security.

"We need Buhari to clear Boko Haram out of Nigeria," he said as a dozen others crowded around him, waving flashlights and shouting "Buhari!"

Fatima Umar, a 20-year-old student, grinned as she sauntered down the street with two friends, all holding Buhari posters in front of them.

"My future is not so bad anymore. We have a change of president, "she said.

Asked what she hoped Buhari would bring, she said: "Security, schools, water, roads, everything!"

Gbenga Akingbule in Abuja, Nigeria, contributed to this article.

Write to Patrick McGroarty at patrick.mcgroarty@wsj.com and Heidi Vogt at heidi.vogt@wsj.com