US President Donald Trump's ex-lawyer will return to home confinement after a judge ruled he was sent back to jail in retaliation for writing a tell-all book.
Michael Cohen had been released in May over Covid-19 concerns from a three-year sentence for charges including Trump campaign finance violations.
Authorities this month had said Cohen resisted the terms of his confinement.
Cohen argued his re-arrest was retaliation for his plans to publish a critical book about Donald Trump.
He said that probation officers had asked him to sign a release agreement that would prohibit him from speaking to, or through, any media. When he questioned this order, he was taken back into custody.
US Judge Alvin Hellerstein issued the ruling during a tele-hearing on Thursday.
He said that Cohen's return to jail was "retaliatory because of his desire to exercise his First Amendment rights to publish a book and to discuss anything about the book or anything else he wants on social media and with others".
The Manhattan judge added that, in 21 years on the bench, he had never seen such a provision for a prisoner. "How can I take any other inference but that it was retaliatory?"
Cohen was re-arrested on 9 July - days after he tweeted that he was "close to completion" of his tell-all book relating to the president. The book reportedly details alleged racist comments made by Trump.
This week, Cohen sued US Attorney General William Barr and the prison bureau director over the so-called "gag order".
The suit, brought by Cohen's attorneys and the American Civil Liberties Union (ACLU), claimed that Cohen's re-jailing over the order violated his constitutional right to free speech.
According to court filings, Cohen's book would provide "graphic and unflattering details about the President's behaviour behind closed doors", including descriptions of his "pointedly anti-Semitic remarks and virulently racist remarks" against former President Barack Obama and Nelson Mandela.
The White House has not yet commented on these allegations.
Prosecutors on Wednesday argued that Cohen had been combative during the discussion and took issue with nearly all aspects of the home confinement agreement, US media report.
The judge has ordered prosecutors and Cohen to renegotiate his confinement terms within a week.
Cohen has temporarily agreed to the current confinement order so that he can return home immediately, his lawyer said, according to Reuters.
The 53-year-old will be released home this afternoon, local time, from the Otisville federal prison following a Covid-19 test.
"Today's important ruling recognises that individuals retain their right to speak even when they are incarcerated," said ACLU attorney Vera Eidelman.
Cohen has served a year of his three-year sentence.
The former fixer admitted to lying to Congress about a Trump Tower project in Moscow, and to campaign finance violations for his role in making hush money payments to women alleging affairs with Mr Trump. He also admitted to other tax and bank fraud charges unrelated to the president.
The Department of Justice earlier declined to comment on the matter.