Brazil's ex-President Luiz Inacio Lula da Silva, better known as Lula, is to remain in prison after a day of legal wrangling.
On Sunday, an on-call judge ruled that Lula should be released, but Judge Joao Pedro Gerbran Neto rejected this.
Eduardo Thompson Flores, the head of the federal court that convicted Lula, has now stepped in and said that there is no legal basis for his release.
Lula says he is innocent and the case is politically motivated.
His conviction was part of an anti-corruption investigation known as Operation Car Wash, which has implicated top politicians from several parties.
The initial announcement by Rogerio Favreto – the weekend duty judge – came after members of Lula's Workers Party challenged his jailing in an application on Friday.
Mr Favreto had said the politician should be freed from jail while he appeals a 12-year sentence for corruption and money laundering.
"With all due respect, [Mr Favreto] has absolutely no qualified authority to override the decision," responded Federal Judge Sergio Moro, who has been the driving force behind Operation Car Wash.
He told police to wait for clarification on the legal impasse.
Judge Joao Pedro Gerbran Neto then overruled the decision, ordering police and jail authorities not to take any action.
Mr Faverto then raised his order for the third time, but the one-hour deadline he imposed on police passed without any move being made to free the ex-president.
The head of the court's intervention means that Lula will remain in prison.
A polarising figure
Lula, who led the country from January 2003 to December 2010, is a divisive figure.
Thousands of people took to the streets earlier this year to call for his immediate imprisonment, while his supporters also turned out in large numbers to insist he should be freed.
His jailing was controversial as defendants in Brazil were previously allowed to remain free until their final appeal had been exhausted.
However, the Supreme Court sided with a 2016 ruling from a lower court, under which defendants could be sent to jail after a failed first appeal.
Could he return as president?
Polls conducted before Lula was jailed in April suggested he was the frontrunner for October's presidential elections.
Many political analysts say he will be disqualified from standing again because of his criminal conviction.
But this did not stop his supporters launching a campaign with an event last month.
Judge Faverto cited this as a reason for him to be released, saying it counted as a new development and all candidates should be free in order to protect "the democratic process".
However, others disputed this, saying Lula's intention to run was long known and nothing had changed to provoke a sudden change in his status.
The electoral court will make an official decision in August on whether Lula can run.
Why was he jailed?
In 2014, after Lula left office, prosecutors started investigating allegations that executives at the state oil company Petrobras had accepted bribes in return for awarding contracts to construction firms.
He was convicted of receiving a renovated beachfront apartment worth some 3.7m reais ($1.1m; £790,000) as a bribe by engineering firm OAS.
The defence says Lula's ownership of the apartment has never been proven and that his conviction rests largely on the word of the former chairman of OAS, himself convicted of corruption.
Lula lost his first appeal in January, when the appeals court not only upheld his conviction but increased the sentence from nine-and-a-half years to 12.