By Peter Baker and Katie Benner, New York Times–

President Trump forced out Attorney General Jeff Sessions on Wednesday and replaced him with a loyalist who will now take charge of the special counsel investigation into Russia’s election interference, a defiant move just a day after a midterm election loss.

Mr. Sessions delivered his resignation letter to the White House at the request of the president and Mr. Trump tapped Matthew Whitaker, Mr. Sessions’s chief of staff, as acting attorney general. In that capacity, Mr. Whitaker assumes control of the Russia investigation, raising questions about the future of the inquiry led by the special counsel, Robert S. Mueller III.

Mr. Whitaker has previously questioned the scope of the investigation. In a column for CNN last year, Mr. Whitaker wrote that Mr. Mueller would be going too far if he examined the Trump family’s finances. “This would raise serious concerns that the special counsel’s investigation was a mere witch hunt,” Mr. Whitaker wrote, echoing the president’s derisive description of the investigation. Mr. Mueller has subpoenaed the Trump Organization for documents related to Russia.

Until now, the investigation has been overseen by Rod J. Rosenstein, the deputy attorney general, because Mr. Sessions recused himself, citing his active role in Mr. Trump’s 2016 campaign. Because Mr. Whitaker has expressed opinions about the investigation, Justice Department ethics advisers may be asked to weigh whether he should also recuse himself. If that were to happen, Mr. Rosenstein would continue to oversee the special counsel.

Mr. Whitaker had no plans to make any immediate public comments about Mr. Mueller, an administration official said.

[Read our profile from September of the acting attorney general, Matthew Whitaker.]

The ouster of Mr. Sessions came just a day after midterm elections that handed control of the House to Democrats, dealing a major blow to Mr. Trump for the final two years of his term. Republicans preserved their hold on the Senate and increased their majority slightly, making it likelier that Mr. Trump will be able to confirm a replacement.

But House Democrats have made clear that they plan to use the subpoena power that will come with their majority to reopen the lower chamber’s own investigation into the Russia matter.

The ouster of Mr. Sessions ended a partnership that soured almost from the start of the administration and degenerated into one of the most acrimonious public standoffs between a commander in chief and a senior cabinet member in modern American history.

John F. Kelly, the White House chief of staff, called Mr. Sessions before his postelection news conference on Wednesday to tell the attorney general that Mr. Trump wanted him to step down, the administration official said. Mr. Trump, who did not speak with Mr. Sessions himself, then ducked questions about Mr. Sessions’s fate at the news conference.

Mr. Sessions then had his letter, which was undated, delivered to the White House. “Dear Mr. President, at your request I am submitting my resignation,” he wrote. He added, “Most importantly as my time as attorney general, we have restored and upheld the rule of law,” and thanked the president.

Mr. Trump announced the resignation and Mr. Whitaker’s assignment on Twitter. “We thank Attorney General Jeff Sessions for his service, and wish him well!” he wrote. “A permanent replacement will be nominated at a later date.”

The president has regularly attacked the Justice Department and Mr. Sessions, blaming the attorney general for the specter of the special counsel investigation into ties between Mr. Trump’s campaign and Russia.

Mr. Trump has said for months that he wished to replace Mr. Sessions, but lawmakers and administration officials believed that firing the attorney general before the midterm elections would have had negative consequences for Republicans in tight races. So it came as little surprise when Mr. Sessions’s resigned the day after the midterms were over.

Mr. Trump blamed Mr. Sessions for recusing himself from overseeing the investigation in its early stages, leading to the appointment of a special counsel.

“He took the job and then he said, ‘I’m going to recuse myself.’ I said, ‘What kind of a man is this?’” Mr. Trump said this year in a Fox News interview. “I wanted to stay uninvolved. But when everybody sees what’s going on in the Justice Department — I always put ‘justice’ now with quotes.”

The deputy attorney general, now Mr. Rosenstein, would normally be in line to become the acting attorney general, but Mr. Trump has complained publicly about Mr. Rosenstein, too.

Installing Mr. Whitaker could clear the way for Mr. Trump to force out Mr. Mueller. To dismiss a special counsel, the president has to order the attorney general or, in the case of a recusal, the deputy attorney general, to carry it out. Mr. Rosenstein has said that he sees no justification to dismiss Mr. Mueller. Mr. Trump has already fired James B. Comey, the F.B.I. director originally overseeing the investigation.

Mr. Whitaker’s ascendance to the top of the Justice Department shows much loyalty means to Mr. Trump. The president has long regarded Mr. Whitaker as his eyes and ears inside a department that he considers an enemy institution.

A former college football player and United States attorney, Mr. Whitaker has been a frequent White House visitor and served as what one White House aide called a “balm” on the relationship between the president and the Justice Department.

In addition to Mr. Mueller’s subpoena of the Trump Organization, an umbrella company that encompasses Mr. Trump’s business ventures, federal prosecutors in Manhattan indicted the president’s former lawyer, Michael D. Cohen, for campaign finance fraud related to payments made on behalf of Mr. Trump. Those prosecutors have secured Mr. Cohen’s cooperation in ongoing investigations, as well as that of Allen Weisselberg, a longtime accountant for the Trump family’s real estate empire.

In pushing out his attorney general, the president cast aside one of his earliest and strongest supporters.

In February 2016, Mr. Sessions became the first sitting senator to endorse Mr. Trump’s presidential campaign, and in the months leading up to the election, he became one of the candidate’s closest national security advisers.

Only weeks after he was confirmed as the United States’ top law enforcement officer, Mr. Sessions became ensnared in the Russia inquiries that have consumed Mr. Trump’s presidency. He recused himself from overseeing the Justice Department investigation in March 2017, after revelations that he had failed to report encounters with Ambassador Sergey I. Kislyak of Russia during the 2016 campaign.

At the time, Mr. Sessions said there was nothing nefarious about those meetings, although he acknowledged that he “should have slowed down” and been more thoughtful in denying any contacts with Russian officials during his Senate confirmation process. His recusal was one of his first public acts as attorney general.

Mr. Trump has long believed that those who have supported and defended him are most entitled to high-ranking positions in the federal government. Mr. Sessions, in Mr. Trump’s mind, had betrayed that axiom.

In a July 2017 interview with The New York Times, Mr. Trump unexpectedly lashed out at Mr. Sessions.

“Sessions should have never recused himself, and if he was going to recuse himself, he should have told me before he took the job and I would have picked somebody else,” Mr. Trump said. He said Mr. Sessions had given some “bad answers” during his confirmation hearings.

Mr. Trump never forgave Mr. Sessions, and over the next year and a half, his complaints about Mr. Sessions on Twitter and in his public comments became more pointed and insulting. At various points, he called Mr. Sessions “beleaguered,” “VERY weak” and “DISGRACEFUL.” In private, he referred to him derisively as “Mr. Magoo,” after the befuddled cartoon character.

Mr. Trump also publicly badgered Mr. Sessions to open investigations into his defeated rival, Hillary Clinton, and other Democrats, and when Mr. Sessions did not, the president excoriated the attorney general. Critics from both parties said the president was shredding the traditional independence of the law enforcement agencies in seeking what appeared to be politically motivated prosecutions.

For the most part, Mr. Sessions made no public retort. But after the president chided him in February for leaving an inquiry into the F.B.I.’s handling of the Russia investigation to an inspector general rather than conducting his own review, Mr. Sessions pushed back. “As long as I am the attorney general,” he said, “I will continue to discharge my duties with integrity and honor.”

In March, Mr. Sessions said he still believed he did the right thing in recusing himself. “I don’t think the attorney general can ask everybody else in the department to follow the rules if the attorney general doesn’t follow them,” he told Time magazine.

When Mr. Trump said that Mr. Sessions “never took control of the Justice Department,” Mr. Sessions fired back hours later, saying in a rare public rebuke that he “took control of the Department of Justice the day I was sworn in.”

“The Department of Justice,” Mr. Sessions said, “will not be improperly influenced by political considerations.”

Mr. Sessions seemed more aligned with the president when he fired Andrew G. McCabe as deputy director of the F.B.I. barely a day before Mr. McCabe was due to retire, jeopardizing his pension. Mr. Trump for months had publicly berated Mr. McCabe, a Republican, because Mr. McCabe’s wife had run for office as a Democrat with financial support from a friend of Mrs. Clinton’s. In firing him, Mr. Sessions cited an inspector general investigation that found that Mr. McCabe had not been fully candid about his interactions with a reporter, an assertion the former deputy director denied.

Mr. Sessions tried to resign at least twice. In June 2017, shortly after his recusal, Mr. Trump berated Mr. Sessions during a private meeting in the Oval Office and accused him of “disloyalty.” Mr. Sessions grew emotional and agreed to resign. Reince Priebus, then the White House chief of staff, later said he ran out of the building to find the attorney general in the parking lot and stop him from leaving.

Ultimately, Mr. Priebus persuaded Mr. Trump not to accept the resignation. Mr. Priebus said he intervened again to save Mr. Sessions a couple of months later when the president again demanded a resignation. “If I get this resignation,” Mr. Priebus remembered telling Mr. Trump, “you are in for a spiral of calamity that makes Comey look like a picnic.”

As attorney general, Mr. Sessions made a forceful mark on the Justice Department. He rolled back some of President Barack Obama’s signature policies as he encouraged federal prosecutors to pursue the toughest possible charges and sentences against criminal suspects. He successfully advised Mr. Trump to rescind Mr. Obama’s program protecting nearly 700,000 young immigrants who were brought to the country illegally as children. He sued California over its sanctuary laws and targeted states that legalized marijuana.

Mr. Sessions, 71, got his start in politics as a United States attorney in Alabama, but his nomination for a federal judgeship was blocked by the Senate amid charges of racial insensitivity. He mounted a comeback by winning election as the state attorney general and then, in 1996, to the Senate that had once rejected him.

Rebecca R. Ruiz and Michael D. Shear contributed reporting.