The Gambia’s president of 22 years Yahya Jammeh will be replaced by a property developer, Adama Barrow, after losing the general election.
Mr Jammeh, who came to power in a coup in 1994, has agreed to accept defeat, said electoral commission chief Alieu Momar Njie.
Before announcing the final result, Mr Njie appealed for calm as the country entered unchartered waters.
The Gambia has not had a smooth power transfer since independence in 1965.
Mr Njie said that Mr Barrow had won Thursday’s election by more than 50,000 votes. He runs a property company which he founded in 2006.
A devout Muslim, Mr Jammeh, 51, once said he would rule for “one billion years” if “Allah willed it”.
“It’s really unique that someone who has been ruling this country for so long has accepted defeat,” Mr Njie told reporters.
Analysis: BBC World Service Africa editor Mary Harper
Mr Jammeh’s defeat has been greeted with astonishment in The Gambia, where most people expected him to win. He has served four terms as president but now this unpredictable and ruthless man is to be replaced by a property developer.
Mr Jammeh’s 22 years in power have brought repression and intolerance to this tiny seaside nation, popular for cheap holidays in the sun.
He has been tough on journalists, the opposition and gay people. He also said he could cure Aids and infertility.
During the campaign, the country’s mostly young population seemed to be yearning for change, said the BBC’s Umaru Fofana in the capital, Banjul.
The economic challenges the country faces have forced many to make the perilous journey to Europe, with some drowning on the way, he said.
Human rights groups have accused Mr Jammeh, who has in the past claimed he can cure Aids and infertility, of repression and abuses.
Several previous opposition leaders are in jail after taking part in a rare protest in April.
Observers from the European Union (EU) and the West African regional bloc Ecowas did not attend the vote.
Who is Adama Barrow?
- Born in 1965 in small village near the eastern market town of Basse
- Moved to London in the 2000s, reportedly working as a security guard at Argos department store while studying
- Returned to The Gambia in 2006 to set up his own property company
- Won the presidential nomination in 2016 to lead coalition of seven opposition parties
- Has criticised the lack of a two-term limit on the presidency
- Has said he would introduce a three-year transitional government made up from members of the opposition coalition if he wins.
Gambian officials opposed the presence of Western observers, but the EU said it was staying away out of concern about the fairness of the voting process.
The African Union did despatch a handful of observers to supervise the vote, however.
The Gambia, a tiny country with a population of fewer than two million, is surrounded on three sides by Senegal and has a short Atlantic coastline popular with European tourists.