BANJUL (Reuters) - Seconds after the chairman of Gambia’s electoral commission read out the poll results that ended President Yahya Jammeh’s 22-year rule to reporters, a policeman left the building, cheering and clapping his hands in the air.

Court security officer stand near a member of Gambia's Army force bus at the Supreme Court in Banjul, Gambia December 5, 2016. Picture taken December 5, 2016. REUTERS/ Thierry Gouegnon

This celebration of victory in last week’s election for Adama Barrow, a property developer with no previous political experience, drew a rebuke from a fellow officer who reminded the policeman he was in uniform.

“Adama Barrow is my new uniform,” he retorted.

The apparently minor incident pointed to shifting allegiances within the security forces that left Jammeh with little choice but to step aside.

Later that day, the leader who once said he could rule the tiny West African country for “a billion years” and who had crushed numerous coup attempts, stunned viewers by calling Barrow to concede defeat on state TV.

The decision by the quirky but widely feared Jammeh to accept defeat in the Dec. 1 election has been hailed as a victory for democracy on a continent where veteran rulers have clung to power by methods ranging from legal shenanigans and simple cheating to brute force.

But it was the security forces who made clear to Jammeh that he would have to hand power next month to Barrow, a former security guard in a north London discount store.

According to political and security sources, the morning after voting - but before the Independent Electoral Commission announced the results - the heads of the army, police and National Intelligence Agency met Jammeh at his office.

“He was advised by them that he should be ready to officially concede defeat,” said Alhagie Darboe, spokesman for the leading party in Barrow’s coalition, citing one of Jammeh’s ministers.

Jammeh appeared ready to take their advice. “After the meeting, he told his ministers: ‘You will be jobless in January’,” a security source told Reuters.

An aide then informed the electoral commission that he would accept his loss. “Even before the (results) announcement he wanted to concede defeat,” commission chairman Alieu Momar Njai told Reuters.

Slideshow ( 6 images )

So, wearing white robes and looking almost cheerful, Jammeh took to television to announce his retirement from politics. “I will be a farmer,” he said, his long official title - H.E. Sheikh Prof. Dr. Alh. Yahya AJJ Jammeh Babili Mansa - stretching across the width of the screen.

Analysts had weighed the risk that he would instead dig in and order his troops to crush dissent, as they did at a student protest in 2000. Some opposition members had hired bodyguards and spent election night in safehouses awaiting the results.

Jammeh, who along with many of his closest allies in the army is from the Jola ethnic minority, had previously vowed to crush opposition among the larger Madinka group.

But in the end, the security forces acted to prevent the kind of post-election bloodshed that hit Burundi in 2015-16.

Army head General Ousman Badjie declined to comment on the meeting, but he told Reuters: “I am just here to keep the country safe and stable. I am here for the Gambian people, I am not a politician.”

Badjie has pledged allegiance to Barrow, the president-elect’s spokeswoman said.

“NOT THE BAD GUYS”

Gambians - who have lived for so many years in a virtual police state and never known a peaceful transfer of power since independence from Britain in 1965 - exploded with joy and celebrated the election result for days.

Wearing yellow “Fear No More” T-shirts, they cheered and banged drums atop vans in the capital Banjul, delighting many European tourists there for winter sun.

Officers joined them or honked at revelers following the elections when Gambians had cast ballots by dropping marbles into colored drums representing each of the candidates. Results from polling stations next to barracks, where many soldiers had voted in uniform, showed a Barrow victory.

Diplomats say they would have looked to neighbor Senegal, which entirely surrounds the riverside nation and shares linguistic and ethnic ties, to intervene had violence broken out - as it did in 1981 during a coup.

Analysts say that the repression of Jammeh’s political opponents had backfired, helping a medley of opposition parties to rally behind a single leader for the first time, with Barrow eking out a victory by fewer than 20,000 votes.

Some prisoners, however, were freed from the Mile 2 prison near Banjul in a surprise gesture last year, including many former top military brass accused of treason.

“The soldiers are not the bad guys. They have been made bad by his orders,” said Jaina Ba Jetta, whose father was jailed for years in Mile 2 for “security reasons” and now lives in exile.

“There is constant fear, no security of tenure and everyone in the army is fed up,” said Francis Mendy from the Gambia office of West African peacebuilding body WANEP.

Even had they wanted to, it would have been hard for officers to seize power themselves on Dec. 1 because of a deliberate “divide and rule” strategy with armories broken up into small depots, he added.

Officials from Barrow’s coalition say that modernizing Gambia’s bloated army, whose checkpoints are dotted across the capital, will be among the priorities.

“The military needs to become a highly trained force that shows loyalty to the people and the constitution,” said Mai Ahmad Fatty, the head of Barrow’s transition team.