United States President Trump said on Monday that Attorney General William P. Barr would depart next week, ending a tenure marked by Mr. Barr’s willingness to advance the president’s political agenda and criticism that he eroded the post-Watergate independence of the Justice Department.

Mr. Barr had in recent weeks fallen out of favor with the president after acknowledging that the department had found no widespread voter fraud, but Mr. Trump sought to play down their differences, saying in a tweet announcing Mr. Barr’s departure, “Our relationship has been a very good one, he has done an outstanding job!”

Still, his resignation allows Mr. Barr to avoid any confrontation with the president over his refusal to advance Mr. Trump’s efforts to rewrite the election results.

Mr. Barr praised Mr. Trump in a resignation letter for overcoming what the attorney general said was an unprecedented effort by his political opponents to take down the president.

“No tactic, no matter how abusive and deceitful, was out of bounds,” Mr. Barr said. “The nadir of this campaign was the effort to cripple, if not oust, your administration with frenzied and baseless accusations of collusion with Russia.”

Jeffrey A. Rosen, the No. 2 at the Justice Department, will take over as acting attorney general when Mr. Barr leaves on Dec. 23, and Richard Donoghue, an official in Mr. Rosen’s office, will become the deputy attorney general.

Mr. Barr, 70, who also served as attorney general in the George Bush administration, was viewed initially in Washington as a stabilizing force in the chaotic Trump era, but that expectation dissipated as he took aim at the Justice Department’s own investigation into the Trump campaign’s ties to Russia that had long antagonized the president.

Mr. Barr brought the Justice Department closer to the White House than any attorney general in a half-century. Defying the distance that federal law enforcement officials have typically maintained from campaign politics, Mr. Barr spent the months leading up to the election echoing Mr. Trump’s unsubstantiated claims of widespread voter fraud. He also told an interviewer that the country would be “irrevocably committed to the socialist path” if the president were not re-elected.

But he backed off the warnings of voter fraud after the election, saying little publicly for weeks until he said that the department had received no evidence that would overturn Joseph R. Biden Jr.’s election. “To date, we have not seen fraud on a scale that could have effected a different outcome in the election,” Mr. Barr told The Associated Press.

That departure from the president was a rare step for Mr. Barr, who had worked to undermine the most significant conclusions of the Russia investigation. Weeks after taking office, he released a summary of the report by the special counsel, Robert S. Mueller III, that a judge later called distorted and misleading, and he held a news conference just before the full report was released where he described it in the best possible light for Mr. Trump.

Mr. Barr appointed a special prosecutor, John H. Durham, to inspect whether the inquiry was wrongfully opened and he sought the withdrawal of the prosecution of Michael T. Flynn, the president’s first national security adviser. He overruled prosecutors who requested a tough sentencing recommendation for Roger J. Stone Jr., one of Mr. Trump’s longtime advisers.

Mr. Trump also handed him sweeping declassification powers to learn about any intelligence gathered in 2016 about Russia’s election interference, giving Mr. Barr leverage to root around at the C.I.A. and other intelligence agencies.

His tenure prompted a handful of career prosecutors to publicly criticize him, highly unusual actions that flouted Justice Department rules prohibiting employees from publicly discussing sensitive internal matters.

“Prosecutors are supposed to do their jobs without regard to party or politics,” Michael Dion, a prosecutor in Seattle, wrote in a letter to the editor in The Seattle Times. “Barr, however, is turning the Justice Department into a shield to protect the president and his henchmen.”

Mr. Barr publicly broke with Mr. Trump rarely; before his dismissal of voter fraud claims, the most prominent example came during the fight over Mr. Stone’s sentencing recommendation. After prosecutors recommended that Mr. Stone be imprisoned for seven to nine years for obstructing a congressional inquiry that threatened to embarrass the president, Mr. Trump publicly called it “horrible and very unfair.”

Mr. Barr’s intervention hours later prompted widespread criticism that the Justice Department was bending to White House pressure. In an effort to quell the criticism, Mr. Barr publicly responded that Mr. Trump’s comments made his job “impossible” by impairing his ability to act without facing accusations of bowing to political interference.

Mr. Trump rebuffed him, continuing to assail the criminal prosecution of Mr. Stone and other issues related to the Russia investigation. But he did not fire Mr. Barr, who was also said to have considered resigning. Some suggested the president had mollified him by agreeing not to cite him in a way that made it seem like he was simply Mr. Trump’s lackey.

Mr. Barr took over the Justice Department after the president forced out Jeff Sessions as attorney general in November 2018, reassuming a position Mr. Barr held roughly a quarter of a century ago under President George H.W. Bush. He quickly became one of the most powerful members of Mr. Trump’s cabinet.

He swiftly used his discretion to disclose nearly all of a 448-page report by Mr. Mueller on Russian interference in the 2016 election. That decision gave some critics of Mr. Trump hope that the new attorney general would help curb the president’s excesses and protect the department from political interference.

But that faded as Mr. Barr made decisions that dovetailed precisely with Mr. Trump’s wishes and the demands of his political allies. In ever stronger terms, he attacked the F.B.I.’s investigation, instigating first a review, then criminal inquiry. Like the president himself, he suggested the inquiry was an abuse of the F.B.I.’s power.

Despite the fact “there was and never has been any evidence of collusion,” Mr. Barr said last December, “the president’s administration has been dominated by this investigation into what turns out to be completely baseless.”

“And the question really is, what was the agenda after the election that kept them pressing ahead after their case collapsed? He’s the president of the United States,” he said.

Independent reviews have found that investigators opened the Russia inquiry without political bias. The investigation uncovered an elaborate Russian campaign to sabotage the 2016 campaign, the president’s repeated efforts to thwart the inquiry and the Trump campaign’s expectation that it would benefit from the Kremlin operations.

After a scathing report by the inspector general illuminated serious omissions and errors in the F.B.I.’s applications for warrant to wiretap Carter Page, a onetime Trump campaign adviser, Mr. Barr imposed new restrictions on inquiries directed at presidential campaigns, requiring his personal approval of any inquiry into a 2020 presidential candidate.

An unusually strong advocate of expansive presidential powers, Mr. Barr asserted that the administration had a wide legal berth to fight congressional subpoenas. His broad view of the executive branch’s authority, well known before Mr. Trump appointed him, made him a favorite target for Democrats in Congress. But it endeared him to some Republicans, and to the president, who had publicly complained that Mr. Sessions was too weak to stand up for him for months before he fired him.