President Trump on Wednesday put Attorney General William P. Barr squarely in the middle of the brewing controversy over the Justice Department’s sentencing recommendation for Trump’s longtime friend Roger Stone, praising Barr for seizing command of the case from career prosecutors.

The president’s Twitter message came just a day after the Justice Department was again thrust into a political firestorm, when the four prosecutors on the Stone case withdrew from the proceedings amid a dispute over what penalty they should propose for the president’s close associate.

Legal analysts and others said the episode represented a low moment for the agency, suggesting that its Trump-appointed leaders were bending to the president’s political whims and trying to undermine the last prosecution brought by former special counsel Robert S. Mueller III as part of his investigation of Russian interference in the 2016 presidential election.

Trump, though, made clear he was pleased with the department’s moves — and with Barr, in particular.

“Congratulations to Attorney General Bill Barr for taking charge of a case that was totally out of control and perhaps should not have even been brought,” Trump wrote. “Evidence now clearly shows that the Mueller Scam was improperly brought & tainted.”

A Justice Department spokeswoman declined to comment on the president’s statements.

Some current and former Justice Department officials have long feared that Barr is willing to risk the institution’s historic independence to serve an irascible president. The top Democrat in the Senate called for the Justice Department inspector general to investigate the Stone episode, and the House Judiciary Committee announced Wednesday it would have Barr testify March 31 to address that case and other recent incidents that it said “raise grave questions” about Barr’s leadership.

Among those is Barr’s recent acknowledgment that he had created what he called an “intake process” for Trump’s personal lawyer Rudolph W. Giuliani to give the Justice Department what Giuliani has claimed is damaging information about former vice president Joe Biden and his family.

Two people familiar with the matter said Barr has instructed the U.S. attorney’s office in Pittsburgh to handle such information. Giuliani’s claims are particularly problematic for the Justice Department because he is the president’s lawyer, and under investigation by federal prosecutors in New York for his business ties to two men accused of breaking campaign finance laws.

Barr also has faced criticism for his handling of the Mueller probe — particularly those cases, including the prosecution of Stone, that have continued after the closure of the special counsel’s office last year.

Stone was convicted by a jury in November of obstructing Congress and witness intimidation. Career prosecutors on the case — working out of the U.S. attorney’s office in the District — on Monday filed their recommendation on what penalty Stone should face when he is sentenced Feb. 20.

That recommendation, though, proved to be thornier than most, as the career prosecutors sparred with their supervisors over what was appropriate. The prosecutors argued the sentencing guidelines called for seven to nine years in prison. Political leadership at the Justice Department, though, pushed for something less, arguing Stone’s conduct did not merit a lengthy addition to his sentence for threatening violence.

On Monday, it seemed the career prosecutors had won out. All four signed on to a recommendation, also endorsed by interim U.S. attorney for the District of Columbia Timothy Shea, that recommended a sentence based on a guidelines calculation. The move enraged Trump, who tweeted early the next morning: “This is a horrible and very unfair situation. The real crimes were on the other side, as nothing happens to them. Cannot allow this miscarriage of justice!

Hours later, a senior Justice Department official claimed department leadership was “shocked” at the recommendation and would move to undo it. All four career prosecutors moved to withdraw from the case, with one quitting the government entirely. The Justice Department then filed a new recommendation — signed by Shea and a different career prosecutor — that said the previous guidance “could be considered excessive and unwarranted under the circumstances.” It did not advocate for a specific penalty but suggested three to four years in prison would be reasonable.

Kerri Kupec, a Justice Department spokeswoman, said that the department had decided before Trump’s Tuesday tweet to revise the recommendation and that there were no discussions between the White House and Justice Department about the Stone case in the days leading up to the prosecutors’ guidance.

A Justice Department official said senior leaders at the agency had expected the first Stone filing to say what the second filing did.

Officials have not provided a clear timeline of the interactions between career prosecutors and department leadership, or fully explained how leadership could have been taken aback by the initial sentencing recommendation. Shea was a counselor in Barr’s office before he was named as interim U.S. attorney last month. Also involved in the discussions was David Metcalf, who had been a counselor in Deputy Attorney General Jeffrey A. Rosen’s office and now works for Shea.

A spokesman for the U.S. attorney’s office declined to comment or address questions about Shea and Metcalf’s handling of the matter.

The Stone episode comes just weeks after the U.S. attorney’s office for the District also seemed to soften its stance on another case originally brought by Mueller against former Trump national security adviser Michael Flynn, who pleaded guilty to lying to the FBI about his contacts with Russia’s former ambassador to the United States.

In early January, prosecutors recommended that Flynn be sentenced “within the Guidelines range” of zero to six months in prison. But in another filing just weeks later, they made clear they agreed with Flynn that a sentence of probation is “reasonable.”

Prosecutors did not explain in the later filing why they emphasized probation as a reasonable sentence for Flynn. Both documents were signed by career prosecutors — Brandon L. Van Grack and Jocelyn Ballantine. Flynn is now seeking to withdraw his guilty plea, alleging a variety of government misconduct.

Spencer S. Hsu and Ann E. Marimow contributed to this report.