Former Brazilian president Michel Temer was detained on allegations of corruption

Brazil's ex-president and alleged head of a graft ring, Michel Temer, was freed from jail Monday after a judge said there were no legal grounds for his detention.

Temer, accused of leading a sophisticated embezzlement and money laundering scheme, was the second ex-president of the Latin American country to be caught up in a sprawling anti-corruption probe called Car Wash that has claimed scores of political and corporate scalps.

The 78-year-old predecessor of the current far-right President Jair Bolsonaro was arrested Thursday in Sao Paulo and placed in "preventative detention" in Rio de Janeiro -- a decision overturned by judge Antonio Ivan Athie, citing the lack of legal justification.

Athie also ordered the immediate release of former mines and energy minister Moreira Franco and several others detained along with Temer.

"I am not against the so-called Car Wash (investigation)," Athie said in his decision.

"I want to see our country free from the corruption that plagues it. However, without constitutional guarantees, guaranteed to all ... there is no legitimacy in the fight against this plague."

Despite evidence that could incriminate the accused, it "did not justify preventative detention", Athie said, pointing to the absence of proof that the accused were a threat to public order or would hide evidence.

Hours after the ruling, Temer was freed and whisked away in a car.

The federal prosecutor's office said it would appeal the decision.

- 'Various crimes' -

The federal prosecutor's office on Thursday accused Temer of leading a criminal organization that was "involved in concrete acts of clear gravity."

It described a "sophisticated criminal system" which by the end of 2014 had made illicit payments of some one million reais (the equivalent of $500,000 at the time) in connection with the construction of a nuclear power plant that was never completed.

Investigations suggest the group may have committed "various crimes" with public agencies and state-owned companies involving nearly half a billion dollars, the statement said.

Since 2014 the Car Wash probe has uncovered a vast graft operation involving state oil firm Petrobras and major construction companies and bribes to politicians of several parties.

More than 150 people have been convicted and hundreds more charged so far. The scale of the corruption uncovered has stunned Brazilians weary of graft among their leaders.

Temer is just the latest ex-president caught up in the probe.

Former leftist president Luiz Inacio Lula da Silva is serving a lengthy jail term after being convicted in two corruption cases.

Temer, who took over as a caretaker figure after the last Workers Party president, Dilma Rousseff, was impeached and removed from office in 2016, was Brazil's most unpopular leader ever.

While Rousseff was not directly implicated in the Car Wash scandal, she was damaged by association, having chaired the board at Petrobras between 2003-2010, when much of the alleged corruption was flourishing.

Temer, a center-right politician of the PMDB party, faced several corruption accusations on leaving office last year.

Brazil's Congress voted to dismiss two allegations in 2017, cementing his reputation as the ultimate survivor in one of the world's messiest and most scandal-ridden democracies.