The despot Yahya Jammeh once said he was prepared to stay in power for a billion years if that’s how long it took to execute his vision for Gambia, the tiny West African nation he has ruled.

On Thursday, in a stunning upset, voters decided that 22 years had been plenty. They elected as president Adama Barrow, a little-known real estate developer who became the standard-bearer of the opposition after several of its leaders were detained. Mr. Jammeh, who has led the nation since 1994, after he helped stage a coup, briefly considered disavowing the results. But he conceded and has vowed to hand over power, raising the prospect that radical reforms will take root in one of the world’s most authoritarian nations.

Mr. Barrow’s victory was a triumph for democracy on a continent where autocratic rule is the norm and peaceful transitions of power have become increasingly rare.