Rio de Janeiro (AFP) - A Brazilian appeals court judge Sunday ruled former president Luiz Inacio Lula da Silva must remain in jail, in a dizzying day of judicial orders and counter-orders months before the country's presidential vote.

Though he is serving a 12-year sentence for corruption, the wildly popular leftist Lula, 72, continues to lead opinion polls ahead of October's election and has vowed his name will be on the ballot.

In his Sunday afternoon ruling, Judge Pedro Gebran Neto overturned a shock order to free Lula, which dropped hours earlier from Judge Rogerio Favreto at an appeals court in the southern city of Porto Alegre -- the same one that had ordered the ex-president's arrest.

Favreto, the weekend duty judge, had ruled in favor of several deputies of Lula's Workers' Party. On Friday they submitted a habeas corpus application on the former president's behalf, arguing he had been illegally imprisoned.

On the heels of the first ruling, top anti-corruption judge Sergio Moro -- who originally sentenced Lula in July 2017 -- said Favreto did not have the power to secure the leftist's release.

Gebran Neto followed suit, instructing federal police at a prison in Curitiba city to keep Lula behind bars.

Yet another twist emerged as the tense afternoon wore on.

- 'Lula Free' -

Favreto insisted his previous order stood, and ordered that Lula should be released within an hour's time.

That deadline passed, and at 6:30 pm (2130 GMT), Lula remained locked up. More than 1,000 supporters gathered outside his prison in Curitiba, waving banners, Brazilian flags and signs calling for his freedom.

Finally the president of the appeals court Carlos Eduardo Thompson Flores ended the drama for now, ruling that Lula should stay in jail by the earlier ruling.

Lula has been imprisoned since April following his conviction for accepting a seaside apartment as a bribe from Brazilian construction company OAS.

He has insisted on his innocence and branded the corruption accusations a political conspiracy aimed at thwarting his electoral aspirations.

Story continues

After ruling Brazil from 2003 to 2011, Lula left office with sky-high ratings following an economic boom and widely praised social programs to reduce poverty.

"Lula free now!" read his Twitter account after Favreto's order, expressing hope for "the end of the illegal imprisonment of Lula."

But even if released, Lula could see his candidacy invalidated by the South American country's electoral court.

Brazilian courts have repeatedly stifled his many efforts to secure freedom.

Just last month Brazil's Supreme Court withdrew Lula's appeal from its agenda, after a lower appeals court ruled the prison sentence could not be referred to the top bench.

The lower court said the top chamber should only handle cases related to possible constitutional violations, a standard which Lula's request did not meet.

- Operation 'Car Wash' -

Prosecutors in May filed new graft charges against Lula, his ex-economy minister and two other political figures allegedly promised $40 million by the Odebrecht conglomerate, which is also linked to corruption scandals elsewhere in Latin America.

The charges are part of operation "Car Wash," Brazil's biggest ever anti-graft crackdown. It has targeted several former presidents, current President Michel Temer and politicians from all major parties.

Investigators discovered that politicians and their parties were allegedly taking money from Odebrecht and other big companies in exchange for political favors and contracts with state oil company Petrobras.

Yet Lula and his supporters remain steadfast in their goal to take back control of Brazil's executive branch.

The former president had been writing football commentary from jail, which a veteran Brazilian sports journalist and leftist sympathizer then read on air.

But last week Lula ceased commenting on the World Cup to comply with electoral rules forbidding potential candidates from making television and radio appearances after June 30.