The relationship between the two men has improved marginally since midsummer, as Mr. Sessions has made a public display of hunting for the leakers among the administration’s national security officials. His allies said that despite the humiliation, the attorney general has stayed in the job because he sees a “once-in-a-lifetime” opportunity as the nation’s top law enforcement official to toughen the country’s immigration policies.
But he may be losing that battle as well. Mr. Sessions played a prominent role announcing the end of the Obama-era program that provided protection to the children of undocumented immigrants, only to see his boss backtrack on the policy. On Thursday morning, Mr. Trump confirmed he had reached a deal with Democrats to provide protections for the so-called Dreamers.
This account is based on interviews with seven administration officials and others familiar with the interactions between Mr. Trump and Mr. Sessions in recent months who requested anonymity because they are not permitted to speak publicly about confidential conversations between the president and his aides. Politico first reported in July that Mr. Sessions had once offered his resignation letter, but the circumstances that prompted the letter — and Mr. Trump’s dressing down of the attorney general — have not previously been reported.
Press officers for the White House and Justice Department declined to comment.
The president’s outburst came in the middle of an Oval Office meeting that Mr. Trump had with top advisers on May 17 to discuss candidates to take over the F.B.I. after the president fired its director, James B. Comey, earlier that month. In addition to Mr. Sessions, Vice President Mike Pence; Donald F. McGahn III; the White House counsel; and several other aides attended the meeting.
In the middle of the meeting, Mr. McGahn received a phone call from Rod J. Rosenstein, the deputy attorney general who had been overseeing the Russia investigation since Mr. Sessions recused himself from the inquiry months earlier. Mr. Sessions had stepped aside after it was revealed he had not provided accurate testimony to Congress about his meetings with the Russian ambassador during the presidential campaign.
In the telephone call to Mr. McGahn, Mr. Rosenstein said he had decided to appoint Mr. Mueller to be a special counsel for the investigation. Congress had been putting pressure on Mr. Rosenstein to appoint a special counsel to put distance between the Trump administration and the Russia investigation, and just the day before The New York Times had revealed that Mr. Trump had once asked Mr. Comey to end the F.B.I.’s investigation into Michael T. Flynn, the former national security adviser.
When the phone call ended, Mr. McGahn relayed the news to the president and his aides. Almost immediately, Mr. Trump lobbed a volley of insults at Mr. Sessions, telling the attorney general it was his fault they were in the current situation. Mr. Trump told Mr. Sessions that choosing him to be attorney general was one of the worst decisions he had made, called him an “idiot,” and said that he should resign.
An emotional Mr. Sessions told the president he would resign and left the Oval Office. That evening, as the Justice Department publicly announced the appointment of Mr. Mueller, the attorney general wrote a brief resignation letter to the president that was later sent to the White House. A person familiar with the events raised the possibility that Mr. Sessions had become emotional because the impact of his recusal was becoming clear.
In the hours after the Oval Office meeting, however, Mr. Trump’s top advisers intervened to save Mr. Sessions’s job. Mr. Pence; Stephen K. Bannon, the president’s chief strategist at the time; and Reince Priebus, his chief of staff, all advised that accepting Mr. Sessions’s resignation would only sow more chaos inside the administration and rally Republicans in Congress against the president. Mr. Sessions, a former Alabama senator, served in the Senate for two decades.
The president relented, and eventually returned the resignation letter to Mr. Sessions — with a handwritten response on it.
For Mr. Sessions, the aggressiveness with which Mr. Trump has sought his removal was a blow. The son of a general store owner in a small town in Alabama, Mr. Sessions had long wanted to be the nation’s top federal law enforcement official or to serve in another top law enforcement or judicial post. He earned a reputation in the Senate as someone tough on immigration, and was the first senator to back Mr. Trump in the presidential campaign.
But their relationship began to deteriorate little more than a month after Mr. Trump was sworn in as president, after Mr. Sessions’s announcement that he was recusing himself from the Russia inquiry caught Mr. Trump by surprise.
The president spent months stewing about the recusal. In a July 19 interview with The Times, Mr. Trump said he never would have appointed Mr. Sessions to be attorney general if he knew he was going to recuse himself from the Russia investigation. Mr. Trump called the decision “very unfair to the president.”
Days after the Times interview, Mr. Trump told aides he wanted to replace Mr. Sessions. Some of the president’s aides, not sure if Mr. Trump really wanted the attorney general gone or was just working through his anger, were able to delay the firing until the president’s anger passed.
But Mr. Trump continued his public attacks in the days that followed, including taking to Twitter to call him “weak” — a word that is among the harshest criticisms in Mr. Trump’s arsenal.
Administration officials and some of Mr. Trump’s outside advisers have puzzled at Mr. Sessions’s decision to stay on. But people close to Mr. Sessions said that he did not leave because he had a chance to have an impact on what he sees as a defining issue of his career: curtailing legal and illegal immigration.
In recent weeks, he has spearheaded the effort to undo what he believed to be the Obama administration’s dangerously lenient immigration policies, including the Deferred Action of Childhood Arrivals program.
Mr. Sessions had no illusions about converting Mr. Trump to his side of the argument — Mr. Trump remains deeply ambivalent — and he had no illusions about repairing a damaged relationship he had once regarded as a friendship. But he told people he felt he had successfully pushed the president toward ending the Obama immigration policy, and thought it had given him increased leverage in the West Wing.
The president agreed to terminate the program, and on Sept. 5 Mr. Sessions stood alone at a lectern — a moment that seemed to be a significant victory for the attorney general.
But his satisfaction was fleeting. Mr. Trump quickly undercut Mr. Sessions in a tweet by saying he would reconsider whether or not to end the program, leading the attorney general to tell allies that he was frustrated that the president had muddled months of work leading to the announcement of the new policy.
On Wednesday evening, Democrats announced they had reached a deal with the president to quickly extend protections for young undocumented immigrants.
On Thursday morning, taking a vastly different position from the one Mr. Sessions had announced, the president tweeted about the need for protections for people brought here “through no fault of their own.”