A military coup brought Buhari to power in late 1983 -- closing a brief period of popular rule by Shehu Shagari -- and another military coup ousted him from power in August 1985.
Buhari's 20-month rule was known for what he described as a "war on indiscipline," a tough regime which some say was marred by human rights abuses.
The 72-year-old retired major general's experience as a military ruler has been viewed as a plus by some and a minus by others in present-day Nigeria, where the government has been locked in a deadly battle with the militant group Boko Haram.
This year alone, the extremists have killed at least 1,000 civilians, Human Rights Watch says.
The ongoing violence in the Northeast has put security -- along with corruption and the economy -- at the top of the election agenda.
Prior to this year's polls in March, Ayo Johnson, a documentary filmmaker and analyst on African affairs, told CNN that voters would opt for whoever could make Nigeria feel safe.
"Many Nigerians will not forget (Buhari) was a military leader, during a dictatorship," Johnson said. "Or maybe they will feel that they need a military leader to address fundamental problems such as terrorism."
Buhari has campaigned as a born-again democrat to allay fears about his strict military regime, while stressing that Nigeria's security needs to be the next government's focus.
"It's a question of security. Whether I was a former military officer or a politician through and through, when there is insecurity of this scale in the country, that takes the priority," he said from his campaign plane.
In another interview with Amanpour in February, Buhari blamed President Goodluck Jonathan's government for repeated setbacks in the fight against extremists.
"The misappropriation of resources provided by the government for weapons means the Nigerian military is unable to beat Boko Haram," he said.
Asked by Amanpour about abuses allegedly committed during his own previous leadership, Buhari said there was "a degree of accuracy" in the claims.
But he said he had ruled Nigeria as part of a military administration.
"When that military administration came under my leadership, we suspended -- as a military then -- part of that constitution that we felt would be difficult for us to operate and as also a consensus," he said. "I think I'm being judged harshly as an individual that what happened during a military administration can be extended under a multiparty democratic system."
Buhari's campaign was fiercely anti-corruption. He ran under the slogan of "new broom," and his supporters were often pictured holding brooms in the lead-up to the vote.
The 2015 presidential race was Buhari's fourth attempt at leadership since he was ousted from power in 1985.
In 2003, Buhari -- then with the All Nigeria People's Party -- lost to Olusegun Obasanjo in an election during which EU observers reported widespread irregularities.
After Yar'Adua's death in 2010, Jonathan rose from vice president to president and Buhari challenged him in the 2011 elections as a candidate from the Congress for Progressive Change.
Buhari had helped found the party a year earlier, saying it was "a solution to the debilitating, ethical and ideological conflicts in my former party, the ANPP."
Buhari is a Muslim from Nigeria's poorer North, while Jonathan hails from a Christian and animist South that is rich with oil.
After Jonathan's victory in 2011, amid accusations of vote-rigging, violent riots broke out in the North.
Armed protesters took to the streets chanting Buhari's name, and more than 800 people were killed in the post-election violence.
Buhari's office issued a statement
calling reports of burning of places of worship places a "sad, unfortunate and totally unwarranted development."
"I must say that this is a dastardly act (that) is not initiated by any of our supporters and therefore cannot be supported by our party," said Buhari's spokesman Yinka Odumakin. "I must emphasize that this is purely a political matter, and it should not in any way be turned into an ethnic, religious or regional one."
Ahead of this year's election, Jonathan and Buhari signed a nonviolence pact, the Abuja Accord
, in January. On March 26 they renewed their pledge and reiterated their commitment to "free, fair and credible elections."
But violent protests broke out
a day before the final results were announced. Protesters fired gunshots and torched a local electoral office in Nigeria's oil-rich Rivers state as they marched to protest the elections, amid claims of vote-rigging and voter intimidation.
Both candidates called for calm, with Buhari, who contested this year's vote as part of the All Progressives Congress, tweeting: "Fellow Nigerians, I urge you to exercise patience and vigilance as we wait for all results to be announced."
After the protests in Rivers, his party demanded the elections there be canceled. But Nigeria's electoral commission decided the results would stand, saying it "did not believe the allegations were substantial enough
to require the cancellation/rescheduling" of the Rivers poll.
Secondary education claims
According to his campaign website,
Buhari is from Daura in Nigeria's northern Katsina state and is married with eight children.
His military training began in 1963 and included stints in the United Kingdom, India and the United States. Buhari was the first chairman of the Nigerian Petroleum Corporation, the site says.
Elements of Buhari's biography were questioned in the run-up to the March 28 election.
After weeks of speculation and an ongoing legal battle
over allegations that Buhari failed to complete his secondary school education, a Nigerian court on March 25 cleared the way for him to run in the presidential race after adjourning the case until April 22.
As a Sunni Muslim from the North, Buhari appears to have moved to address any concerns his appointment could be detrimental to non-Muslim Nigerians -- approximately half the country's population, according to the CIA Factbook.
A blog post on his campaign website
headed "Buhari will never Islamise Nigeria" describes a campaign ceremony in January in Imo state.
Imo Gov. Rochas Okorocha, it says, "dismissed suggestions of plans by Muhammadu Buhari to 'Islamize' Nigeria," telling the audience Buhari's long-time cook and driver were Christians and his youngest daughter had married a Christian.
Buhari has also not been immune to the violence plaguing northern Nigeria. Last year, he was targeted in a suicide bombing
that killed at least 15 people in the city of Kaduna. An earlier blast in the city the same day had been aimed at a Muslim cleric.
A day after winning the country's presidency,
Buhari told CNN's Christiane Amanpour that fighting corruption and curbing violence were at the top of his agenda.
The economy is another major issue
the new leader has to contend with, as the country overtook South Africa last year as the continent's largest economy. But as many as 70% of Nigerians live below the poverty line, surviving on less than a dollar a day.