Gambians are celebrating on the streets of the capital Banjul after the country’s electoral commission announced its controversial president, Yahya Jammeh, had lost the election to opposition leader Adama Barrow. Jammeh, who has ruled the small West African country for 22 years since seizing power in a military coup, confirmed he would step down and work with the new president-elect to help the transition of power.

Over the last two decades, Jammeh has tightened his hold on power with the help of the National Intelligence Agency, which is charged with protecting state security by conducting intelligence and covert investigations. Human rights have been violated and freedom of speech repressed. A 2015 Human Rights Watch report spoke of a “pervasive climate of fear”. Despite this, they went to the polls in huge numbers on December 1 to vote for the 51-year-old Barrow – a former security guard at an Argos catalogue store in London – who had been chosen to lead the opposition coalition.

With no constitutional limit on the number of terms he can serve, it was feared that Jammeh, whose official title is “His Excellency Sheikh Professor Alhaji Dr Yahya A.J.J. Jammeh Babili Mansa”, would win another victory in the elections. But to widespread surprise, Gambia’s electoral commission announced that Barrow had won with 263,515 votes (45.5%). Jammeh got 212,099 votes (36.7%) and Mama Kandeh, from the Gambia Democratic Congress, another opposition party, that was outside Barrow’s coalition, got 102,969 votes (17,8%).

In his election campaign, Jammeh targeted the Gambian youth, promising them “empowerment programmes” to tackle the massive unemployment that drives so many young Gambians to join the dangerous “back way” to Europe and Libya. In November, Fatim Jawara, the goalkeeper of Gambia’s national women’s football team, drowned in the Mediterranean trying to reach Europe.

The stakes for those choosing to vote for Jammeh were high. Weeks before the election, a Gambian opposition leader Ousainou Darboe and 18 other opposition supporters were arrested and sentenced to three years in jail for taking part in a pro-democracy protest in April that the government deemed as illegal.

But in a country where the average age is around 20-years-old, Gambia’s youth could not be persuaded to vote for Jammeh. In an interview with the Guardian, one 25-year-old voter, Haddy Ceesay, said: “Jammeh never gave us what we wanted, but I just know Barrow will be different. He’s listening to us.”

A self-styled Muslim dignitary

A key part of Jammeh’s legacy will be his transformation of the country into an Islamic state that withdrew from international institutions.

My own research has shown how public debates about Islam have evolved since Jammeh, a former army lieutenant, came to power in 1994. Jammeh began to style himself as a Muslim dignitary in the mid-1990s. His statue in the capital, Banjul, shows him in military uniform, but nowadays he appears in public wearing a turban and clasping prayer beads. His supporters proudly referred to him as “The Sultan”.

President Jammeh: known as the ‘Sultan’.Solan Kolli/EPA

Jammeh’s military coup in 1994 caused Europe and the US to temporarily cease their financial support to the Gambia. This prompted Jammeh to establish relations with Islamic powers in the Arab world, including Saudi Arabia, Kuwait, the United Arab Emirates, and Libya.

Yet prior to 1994, Islam had not been very important in Gambian national affairs. Jammeh’s predecessor, Sir Dawda Jawara, built on the colonial legacy of secularism. Jammeh’s move to make Islam a more central part of Gambian life may be partly explained by his age, ethnicity, and background. At the time of his takeover he was only 29-years-old: in a society where status depends on seniority, this youth worked against him.

Being a Jola, an ethnic group generally regarded as not being closely affiliated with Islam, did not help Jammeh’s position either. Many Mandinka elders, who had been loyal followers of Jawara, strongly objected to the idea of a young Jola soldier holding power. To win their support, Jammeh arguably needed to affirm his Muslim identity, and this facilitated a greater role for Islam in Gambian public life.

The growth in sex tourism on the Gambian coast may also have contributed to Jammeh’s Muslim politics. He claimed that the Gambia’s “national decay” is mainly rooted in the immorality caused by sex tourism, and he therefore instituted widespread moral reforms. In government clean-up campaigns, bumsters – beach boys and cannabis smokers who often grow dreadlocks and offer sexual services to tourists – were targeted and their heads shaved.

Withdrawal from institutions

Addressing his supporters in May 2008, Jammeh said that the Gambia was a country of believers where a “sinful and immoral act as homosexuality” would not be tolerated. He warned all homosexual in the country to leave, noting that legislation on homosexuality would be introduced that was “stricter than that in Iran”. In 2014, a law was passed that meant some homosexual acts could be punished with a life sentence.

Jammeh’s declaration in December 2015 that Gambia was an Islamic Republic continued his efforts to be the country’s moral policeman. He issued the proclamation on the basis that more than 90% of Gambians are Muslim and to distance the country from its “colonial legacy”.

In October 2013, he pulled the Gambia out of the Commonwealth, saying it was a “neo-colonial” institution. The following year, the EU suspended some €13m (£10.9m) of development aid because of alleged human rights abuses by Jammeh’s government. In his most recent move, in late October he decided to pull the country out of the International Criminal Court. Gambia’s information minister justified the decision by saying the court stands for: the “International Caucasian Court for the persecution and humiliation of people of colour, especially Africans”.

It’s still unclear what approach Barrow would take on these issues under his presidency, but during the campaign he said he would introduce a transitional government made up of members of the opposition coalition. The Gambia is known in tourist brochures as the “smiling coast of Africa”. It remains to be seen whether Barrow can bring back the smile on Gambians’ faces.