In this photo taken Saturday, March 28, 2015, opposition candidate Gen. Muhammadu Buhari waves to supporters after casting his vote in his home town of Daura, northern Nigeria. (Ben Curtis/AP)

NIGERIA HAS passed the most critical political stress test it has faced in decades. Last weekend, Africa’s most populous nation held its most competitive race ever for president. Shrugging off a six-week postponement, fears of widespread political violence, ongoing terrorism by the Islamist militant group Boko Haram and doubts about the readiness of the Independent National Electoral Commission , Nigerians staged a credible and generally peaceful contest that has set the stage for a democratic change of power. With numerous critical elections set to occur over the next year and a half across Africa, the outcome offers an important and positive example of a working democracy.

For the first time since Nigeria’s return to civilian democracy in 1999, an opposition candidate for president unseated the incumbent. The electoral commission declared Wednesday that retired Maj. Gen. Muhammadu Buhari defeated President Goodluck Jonathan by a decisive ­margin.

The election was not without problems: Electronic card readers in some areas malfunctioned, and there was evidence of inflated voter turnout in Mr. Jonathan’s strongholds. At least 56 people were killed in incidents around the country, including in attacks by Boko Haram. However, none of these factors called the results into question. The outcome was independently verified using statistical technology, leading to a more transparent process.

Mr. Buhari inherits massive challenges. Nigerians will expect their military to improve the effectiveness of its campaign against Boko Haram . At the same time, the new administration must address the long-standing underdevelopment and poverty in Nigeria’s northern states that helps fuel the insurgency and also find a way to assist the almost 2 million Nigerians internally displaced as a result of Boko Haram’s violence. Plummeting oil prices mean a grim outlook for the country’s economy. Mr. Buhari has also promised to tackle Nigeria’s deeply entrenched political corruption.

The United States has a major interest in the new government’s success. It is the biggest foreign investor in Nigeria, home to Africa’s largest economy, and its past efforts to cooperate on security affairs while also promoting human rights have often ended in frustration.

For now, this is a moment to salute the Nigerian people, many of whom patiently waited for hours to register and cast ballots. Even in areas affected by Boko Haram’s terrorism, displaced people lined up to cast votes. Public campaigns urged politicians and their supporters to refrain from violence, and Mr. Jonathan helped with a prompt concession. It is now up to leaders in both parties to ensure that Nigeria has a smooth transition worthy of its voters.