Mr. Acosta acknowledged that the story line around him was a distraction to Mr. Trump. “The focus needs to be on this economy and on job creation, on the decreased fatalities in the workplace and in mining,” he said. “And going forward, that’s where this administration needs to focus, not on this matter.”

Mr. Trump named Mr. Acosta’s deputy, Patrick Pizzella, to serve as acting secretary of labor when Mr. Acosta’s resignation becomes effective July 19. That will bring to four the number of cabinet agencies led by acting secretaries.

The president had initially told people that he thought that Mr. Acosta did well in explaining why he had agreed to a plea deal in which Mr. Epstein served 13 months in jail after being accused of sexually abusing dozens of young women and girls. But after watching a day of coverage critical of Mr. Acosta and hearing from allies who warned that his labor secretary was always going to be a distraction, Mr. Trump began questioning whether he should keep Mr. Acosta, according to people familiar with the president’s thinking.

Mr. Acosta, according to two people familiar with his thinking, was under the impression that Mr. Trump was pleased with how he explained himself there and parried questions from journalists. Top officials at the Labor Department thought the news conference — which had been requested by Mr. Trump — had helped Mr. Acosta save his job and were taken aback by his abrupt resignation, a decision Mr. Acosta made without consulting a wide circle of advisers, or telling many colleagues of his plans.

But Mr. Acosta, those people said, had never been a natural fit in the circuslike environment of the Trump administration. He viewed the cabinet position as a steppingstone to his real professional goal: a judicial appointment on the 11th Circuit Court of Appeals in Atlanta. Some White House officials thought he was making decisions at the Labor Department with that future in mind, trying not to alienate Senate Democrats whose support he would need in a confirmation hearing.