WASHINGTON — The night before he was fired, Attorney General Jeff Sessions was in high spirits.

He was watching the midterm election results at the Justice Department with his wife and staff members, according to two people present, and enjoying a final, welcome evening of normalcy before President Trump abruptly ended his bumpy tenure on Wednesday as the United States’ highest law enforcement officer.

While Mr. Sessions, 71, did more to carry out Mr. Trump’s agenda than almost any other cabinet official, delivering on immigration and combating violent crime and opioids, he also soured their relationship after only weeks in the position by recusing himself from the Russia investigation. The president, who has made clear that he expected protection from his law enforcement officials, viewed the step as a betrayal.

“The one laudable thing he did was to recuse himself from the Russia investigation, and for that, he is being fired,” said Vanita Gupta, a frequent critic of Mr. Sessions and the chief executive of the Leadership Conference on Civil and Human Rights.

Mr. Trump installed Mr. Sessions’s chief of staff, Matthew G. Whitaker, who had served as a liaison to the White House and gained the president’s trust, as acting attorney general.