Cohen contended in a lawsuit that he was being retaliated against by the federal government, which objected to a soon-to-be-released tell-all book about his experiences with Trump.
Federal Judge Alvin Hellerstein sided with Cohen on Thursday.
"The purpose of transferring Mr. Cohen from furlough and home confinement to jail is retaliatory, and it's retaliatory because of his desire to exercise his First Amendment rights and to publish a book," Hellerstein said.
Cohen, who had been in solitary confinement, according to his lawsuit, will be released Friday afternoon, according to his attorney, Danya Perry. Hellerstein also said the conditions of Cohen's home confinement will be renegotiated.
The Bureau of Prisons said in a statement to ABC News that any decision to remand Cohen to prison as "a retaliatory action is patently false."
"During this process, Mr. Cohen refused to agree to the terms of the program, specifically electronic monitoring," the statement said. "In addition, he was argumentative, was attempting to dictate the conditions of his monitoring, including conditions relating to self-employment, access to media, use of social media and other accountability measures. Mr. Cohen refused to acknowledge and sign the conditions of his transfer of home confinement and was remanded into custody."
"Mr. Cohen’s refusal to agree to those conditions here played no role whatsoever in the decision to remand him to secure custody nor did his intent to publish a book," the agency continued.
The bureau previously said Cohen turned down the offer to remain at home in exchange for the media gag order, but Cohen contends he never made such an agreement and was taken into custody before he and his lawyer could negotiate terms.
Cohen was one of several inmates released from federal prison in May after the Department of Justice raised concerns about COVID-19 in facilities. He was active on social media and teased the release of his book, which he said would include anecdotes of Trump using anti-Semitic and racist remarks.
Cohen also was photographed by the New York Post dining outdoors in early July.
Hellerstein blasted the federal government for issuing new guidelines to Cohen's furlough and home confinement.
"How can I take any other inference but that it was retaliatory?" Hellerstein asked rhetorically during a hearing. "In 21 years of being a judge ... I've never seen such a clause."
"He was put on furlough with no conditions other than hang around your house and be in your neighborhood," the judge added. "All of a sudden when the New York Post article comes out and the Bureau of Prisons understands he's writing a book, and it'll be finished by election time, they impose conditions."
Perry told ABC News that the terms of Cohen's home confinement are still being worked out, but she ruled out another return to federal prison.
"He was ordered to serve the remainder of his sentence on home confinement, so he should not have been remanded two weeks ago," she said. "He should not be remanded again."
Perry said Cohen plans to finish and publish his book.
Two other recent books about the president -- "The Room Where it Happened," by former national security adviser John Bolton, and "Too Much and Never Enough," by Mary Trump, the president's niece -- have drawn legal challenges.
Both were published.
This report was featured in the Friday, July 24, 2020, episode of “Start Here,” ABC News’ daily news podcast.
"Start Here" offers a straightforward look at the day's top stories in 20 minutes. Listen for free every weekday on Apple Podcasts, Google Podcasts, Spotify, the ABC News app or wherever you get your podcasts.