SERGEI KARPUKHIN / REUTERS Russian protest leader Alexei Navalny looks out from a glass-walled cage during a court session in Kirov, July 19, 2013.

On Friday morning, Alexei Navalny, the leader of the Russian opposition movement, was roused out of bed by his prison guards and taken back to court. Just the previous day, he had been convicted of embezzlement, sentenced to five years in a labor colony and led out of the courtroom in handcuffs. So he was not in the mood to expect any pleasant surprises. As he told reporters later on Friday, his first thought while being driven to the courthouse was that the prosecutors wanted to bring a new set of charges against him. Instead, the prosecution asked the judge to release Navalny pending the appeals process in his case, and he was allowed to walk out of the courtroom – not exactly a free man, but free enough to continue his long-shot campaign to become the mayor of Moscow.

The sudden reversal, which legal experts called unprecedented, seemed to have more to do with the mayoral race than any prosecutorial change of heart. According to the Associated Press, Judge Ignatiy Embasinov explained that keeping Navalny locked up would “prevent him from exercising his rights of be elected.” Still, Navalny’s campaign staff were baffled by the move. “They must really need us in these elections,” Navalny’s campaign manager, Leonid Volkov, told TIME. “They need us for their own legitimacy.” He was referring to the Russian authorities, particularly the current mayor of Moscow, Sergei Sobyanin, who has confused observers by clearing the way for Navalny to become his electoral rival.

A close ally and confidant of President Vladimir Putin, Sobyanin served five years as Putin’s chief-of-staff before becoming the mayor of Moscow in 2010. But he wasn’t elected to that post. Instead, the Kremlin appointed him after firing the previous mayor. Last month Sobyanin decided to call early mayoral elections which he scheduled for September. The short notice left his opponents little time to mount a campaign against him.

(MORE: Russian Court Jails Putin’s Most Influential Opponent)

Dozens of them tried to throw their hat in the ring, but only six men cleared all the legal hurdles to get on the ballot. Navalny could not have done it without Sobyanin’s help. The most difficult hurdle, which is known as the “municipal filter,” required every candidate to get the support of 110 out of about 2,000 municipal officials in Moscow, and Navalny only managed to get about 60. So the mayor asked a few dozen officials to give their support to Navalny, easing the upstart candidate onto the ballot. “Muscovites need more of a choice, more points of view,” he said in explaining the decision.

In some ways, this seemed liked an uncharacteristically risky move for one of Putin’s allies, who have tended to aggressively sideline potential threats. Navalny is by far the most popular figure in the anti-Putin movement. Since the movement’s beginnings in 2011 he has led several mass demonstrations in Moscow, some attracting more than 100,000 people, to call for the ouster of Putin and his political party, United Russia, which Navalny famously dubbed “the party of crooks and thieves.” (Sobyanin sits on that party’s Supreme Council.) Last year, Navalny came first in an online election to choose the opposition movement’s Coordinating Council, which he now chairs. He has almost 380,000 followers on Twitter and a blog with a monthly readership of about a million people.

But beyond the narrow segment of yuppies and intellectuals who support Navalny in Moscow and other big cities, few of the voters in Moscow’s majority working-class electorate want to see the blogger in any position of power. A survey released earlier this month by the Levada Center, an independent pollster, found that 78% of respondents who were planning to vote in the Moscow election would cast their ballot for Sobyanin. The same poll found Navalny in a distant second place with a projected 8% of the vote. So the incumbent’s victory looks to be guaranteed regardless of whether Navalny is in detention or not.

Far less certain, however, is how the public will perceive the upcoming vote’s legitimacy, especially if the second most popular candidate were to be taken off the ballot. So the court’s decision on Thursday to throw Navalny in prison for embezzlement – which forced his campaign team to withdraw his candidacy and call for a boycott of the vote – made Sobyanin look “pretty foolish,” says Masha Lipman, a political analyst at the Carnegie Center in Moscow.

(MORE: Putin Turns His Back on Snowden — and His Own Anti-Americanism)

All of Sobyanin’s apparent efforts to give the vote – and thus himself – a legitimate gloss suddenly seemed pointless. Less than two months before the ballot, thousands of people rallied at the Kremlin walls on Thursday to support Navalny and denounce his imprisonment, which rights groups and Western governments were also quick to condemn as politically motivated.

During that protest, which snarled traffic on the intersection closest to the Kremlin gates, the news broke that prosecutors were preparing to ask for Navalny’s release pending appeal. Volkov, the campaign manager, who was standing beside me at the protest as he read this news on his smartphone, looked dumfounded. “Did not see that coming,” he said. Although he was hesitant at first to believe the news, he told me that Navalny would re-enter the race if he was released. The appeals process could take months, leaving Navalny enough time to run in the elections.

A significant Navalny surge in the polls seems unlikely but just in case he does start to become too popular for comfort the authorities have left themselves a safety switch, says Sergei Markov, a long-time Kremlin spin doctor who now heads the political science department at Moscow’s Plekhanov University. If Navalny’s ratings in the polls start to climb above 10%, he says, the appeals process could be accelerated and Navalny thrown back in prison. “That mechanism is in place,” Markov tells TIME. “There is always that option to take him out of the elections.”

But the decision to let him run still carries risks, says Markov, and the fact that Navalny was imprisoned one day and freed the next suggests disagreement inside the ruling elite as to whether those risks are worth taking. “For one group it is more important to ensure that these elections look legitimate,” says Markov, who was one of the Kremlin’s leading political strategists during Putin’s first two terms as President. “For another group it is more important to make sure that this revolutionary is not allowed to gain any more footing in the political process.” The judicial system in this battle seems to play the role of a jailer, says Lipman, rather than the role of independent arbiter that is ascribed to the courts in the Russian constitution.

But whatever the real reason for Navalny’s freedom, he plans to use it to his full advantage. Having regained the use of his Twitter account on Friday, he immediately sent his supporters this message: “Even if this is temporary, let’s use this time to maul the crooks.” In his next tweet, he added: “A deep breath and back to work.” Outside the courthouse, however, he was not naïve about the role assigned to him in these elections. “I’m not some domesticated kitten or puppy who can be thrown out of the elections and then told to participate in the elections,” he told reporters. The option of boycotting the vote is still on the table, he said, if it makes more sense to rob Sobyanin of the legitimacy he seems to crave. But whatever Navalny’s next move, the prospect of a long stint in the labor camps will still hang heavy over his future.