DAKAR, Senegal — In the end, democracy prevailed.
When Gambia’s president refused to give up power, neighboring countries rallied troops and invaded this month, threatening to oust him by force.
The tactic worked, and Yahya Jammeh, the repressive ruler who had lost a presidential election in December, stepped down and left the country, relinquishing power after holding it tightly for more than 22 years.
It was nothing short of a democratic coup. So was it an exception, or proof that African leaders are increasingly willing to demand — and enforce — democracy in the region?
Democracy’s record throughout the African continent has been checkered in recent years. While many West African nations, including Burkina Faso and Ivory Coast, have recently celebrated their first peaceful elections or transitions of power in decades, some have experienced violence and fraud at election time. In other countries, leaders appear poised to hold on to power until death, with little threat of intervention from neighboring countries.
Last month in Ghana, an opposition candidate upset the incumbent in peaceful fashion in an election celebrated by observers for its fairness. In a milestone for the continent, Nigeria, Africa’s biggest economy and most populous nation, handed over power to an opposition candidate last year for the first time since democracy was restored in 1999.
But there have been notable setbacks to democracy, too. In the Democratic Republic of Congo, President Joseph Kabila is still in office even though his term expired last month. An election has been delayed several times and may not happen until 2018. Yet nations in the region have not deployed troops to remove him.
In Gabon last year, violence broke out after President Ali Bongo Ondimba was re-elected amid widespread denunciations of manipulation of the vote. In the Republic of Congo, President Denis Sassou-Nguesso’s re-election came with accusations that he launched airstrikes on opposition strongholds. Last year, Uganda’s president, in power for more than 30 years, was re-elected in a vote marred by irregularities.
In Gambia, the outcome of December’s presidential vote, while a surprise, was considered legitimate by the global community. Even Mr. Jammeh initially agreed, phoning his opponent to graciously concede. But it was a regional banding together against Mr. Jammeh that made all the difference once he made it clear a few days later that he had changed his mind and was refusing to go.
African leaders took such a strong line in Gambia’s case for one chief reason: Mr. Jammeh was an easy target to align against. With a long record of human rights abuses and bizarre, repressive behavior, he had increasingly distanced himself from regional and Western governments. He had jailed journalists and opponents, some of whom were tortured or died in prison, and had been widely disliked for years. Numerous governments had denounced Mr. Jammeh’s administration long before he declared that he would defend his presidency with his security forces.
“He rubbed too many people the wrong way,” said Mohammed Ibn Chambas, the top representative to West Africa from the United Nations. “He didn’t have too many friends.”
It didn’t take long for regional organizations to side with the winner of the election, Adama Barrow. The United Nations, the African Union and the Economic Community of West African States denounced Mr. Jammeh’s actions and called on the government of Gambia “to abide by its constitutional responsibilities and international obligations,” according to a joint statement from the groups. “It is fundamental that the verdict of the ballots should be respected.”
The African Union announced that it would no longer recognize Mr. Jammeh as president as of Inauguration Day. And most important, the Economic Community of West African States — a group that despite its name has been increasingly active in peace and security matters — kept open a channel for a diplomatic solution, but simultaneously announced that it was preparing a regional military force to intervene if Mr. Jammeh did not step down.
No one came to the defense of Gambia, a country with a shaky economy so small it barely registers on the global economic stage.
“Gambia wasn’t strategic or interesting to anyone,” said Alex Vines, head of the Africa program for Chatham House, a research group. “So they were all united.”
The coalition plan worked. Mr. Jammeh was allowed to keep negotiating with regional presidents who had flown in for face-to-face discussions. But at the same time, forces from the coalition entered Gambia. Three days after Mr. Barrow was inaugurated in a slapdash ceremony in the Gambian Embassy in Dakar, Senegal, Mr. Jammeh flew out of the country and into exile.
While the show of force had clearly been planned in advance, the initial military foray across Gambian borders came about unintentionally, according to diplomats versed in activities in the region.
Senegalese troops unfamiliar with the area somehow stepped into Gambia by mistake, they said. Word of their unintentional invasion quickly circulated, adding immediacy to Mr. Jammeh’s decision-making process. When he finally left the country, headed for Equatorial Guinea, Mr. Jammeh was allowed to take a small fleet of luxury cars with him.
The region also had recent history to consider. The outcome of a 2011 standoff in Ivory Coast weighed heavily in the minds of negotiators of the Gambian situation, according to Mr. Chambas of the United Nations.
In Ivory Coast, regional influencers initially sat on the sidelines as President Laurent Gbagbo holed up in the presidential palace and waged an armed fight to stay in power after losing an election. United Nations troops were called in to protect the election victor, Alassane Ouattara, who was trapped in a lagoon-side hotel.
For such a tiny nation, Gambia and its presidential crisis attracted huge attention. For Americans, it offered some similarities. Like the election of Donald J. Trump, Gambia’s vote showed a populist candidate with a real estate background and no political experience riding a wave of frustration over economic inequality.
Both candidates pulled off a surprise victory. And in both elections, voters sought to punish elites who many thought did not deliver during their administrations.
The initial days of Mr. Trump’s administration have been punctuated by angry protesters spilling into the streets upset over the introduction of new policies. But for Mr. Barrow, the honeymoon phase of his presidency is in full swing. He flew into the airport in the nation’s capital over the weekend greeted by throngs of cheering supporters.