WASHINGTON — Andrew G. McCabe abruptly stepped down on Monday as the F.B.I.’s deputy director after months of withering criticism from President Trump, telling friends he felt pressure from the head of the bureau to leave, according to two people close to Mr. McCabe.

Though Mr. McCabe’s retirement had been widely expected soon, his departure was nevertheless sudden. It added to what has already been a chaotic upheaval at the F.B.I. under Mr. Trump, who has responded to an investigation into his campaign with broadside attacks against both the bureau and the Justice Department.

As recently as last week, Mr. McCabe had told people he hoped to stay until he was eligible to retire in several weeks. Instead, he will immediately go on leave and retire on March 18.

In a recent conversation, Christopher A. Wray, the F.B.I. director, raised concerns about a forthcoming inspector general report. In that discussion, according to one former law enforcement official close to Mr. McCabe, Mr. Wray suggested moving Mr. McCabe into another job, which would have been a demotion.