A Senegalese court has cleared the way for opposition leader Ousmane Sonko to be released pending his rape trial in a case that has already sparked deadly protests that threaten to erode the country’s reputation as one of West Africa’s most stable democracies.

Sonko is widely seen as President Macky Sall’s greatest potential political challenger in the upcoming 2024 election and his supporters maintain that the allegations are aimed at derailing his political future.

The ensuing protests, in which at least four people were killed last week, have been accelerated by broader, long-standing grievances with Mr Sall’s administration.

Sonko’s supporters vowed to start three more days of demonstrations on Monday, and some indicated they would push ahead even as the opposition figure was expected to be released from custody later after being formally charged.

Security forces deployed early across the capital Dakar in a show of force aimed at preventing further unrest.

As news of Sonko’s pending release spread, throngs of boisterous supporters descended on the courthouse waving Senegalese flags.

“The conspiracy has failed. Our client will go home,” lawyer Cire Cledor Ly told reporters.

“We have had this result thanks to the mobilisation of the people.”

The violence has been the worst unrest to hit Senegal in nearly a decade, as demonstrators have sought to undermine Mr Sall’s business ties with former coloniser France.

In recent days, crowds have set fire to more than a dozen supermarkets opened by French retailer Auchan, and have targeted Total petrol stations.

The sight of burned-out cars and boarded-up shops is a rarity in Senegal, which has never suffered the military coups and dictatorships that have destabilised so many of its neighbours in West Africa over the past half-century.

The opposition coalition known as M2D has urged peaceful protests this week, urging the president “to take concrete action to meet the democratic demands of the Senegalese people”.

Chief among those was the release of Sonko, charged with rape and making death threats.

The demonstrations first began on Wednesday before Sonko’s initial court appearance for questioning on accusations of rape.

He was detained on the way to the courthouse and arrested for disturbing public order after hundreds of his supporters clashed with police who were blocking unauthorised protests.

Sonko, 46, finished third in the 2019 presidential election with just over 15% of the ballots cast, drawing strong support from younger voters.

However, his message of greater economic independence for Senegal has attracted a wider audience amid the financial hardships of the Covid-19 pandemic amid curfews and other movement restrictions.

Mr Sall easily won re-election in 2019 with more than 58% of the vote.

His opponents fear he will seek to extend his mandate with a third term as presidents in neighbouring Guinea and Ivory Coast did last year though Mr Sall has not commented publicly on his intentions yet.

While Mr Sall has been credited with infrastructure and development projects, his critics say that has come alongside the sidelining of political rivals.

Two other rising political stars have previously faced charges their supporters said were politically motivated.

Karim Wade, the son of the president Mr Sall defeated in 2012, was once widely seen as the heir apparent before he was charged with corruption the following year.

He ultimately spent three years in jail before going into exile abroad in Qatar.

Former Dakar mayor Khalifa Sall, seen as a top contender at one point for the 2019 election, was arrested in 2017 on corruption charges and later pardoned after the vote took place.

The leaders of Senegal’s influential Muslim brotherhoods on Sunday urged protesters to show restraint in the days ahead.

“Every Senegalese has the right to express themselves on the situation in the country, but with respect for institutions and without the destruction of public or private property,” said Serigne Mansour Sy.

“We call for peace and serenity.”