The firing of Mr. Sessions came 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 would 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 abrupt ouster of Mr. Sessions resembled in some ways the decision by President George W. Bush to oust Defense Secretary Donald H. Rumsfeld in 2006 the day after a similar electoral defeat in midterm elections. In that case, Mr. Bush was attempting to mollify his critics. Mr. Trump’s decision to fire Mr. Sessions appeared likely to inflame his adversaries on Capitol Hill.

John F. Kelly, the White House chief of staff, called Mr. Sessions before the president’s 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, in 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.”

Though Mr. Trump has said for months that he wished to replace Mr. Sessions, 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 was asked to resign the day after the midterms were over.