“When you get zero from the other side — they let us down because they’re hurting the people,” Mr. Trump said in a telephone interview shortly after he had agreed to pull the measure. Asked whether he was worried the loss would hurt Republicans, he said, “I’ll let you know in a year.”

The demise of the American Health Care Act played out in a tense 24 hours that White House and congressional officials said proved a political education for Mr. Trump and his top advisers on the promise and peril of governing, even with unified Republican control. This account is based on government officials who were present during the last-minute negotiations and who spoke on the condition of anonymity.

“We all learned a lot,” Mr. Trump told reporters on Friday afternoon. “We learned a lot about loyalty, and we learned a lot about the vote-getting process.”

“Certainly for me, it was a very interesting experience,” he added.

Mr. Trump, who initially had little involvement in crafting the health care bill, became more deeply engaged in recent weeks, promoting it at rallies outside Washington and holding meetings in the West Wing with conservative and moderate coalitions whose support was crucial to its passage.

But he made little secret of his ambivalence about addressing the issue — “I would have loved to have put it first, I’ll be honest,” Mr. Trump said of tax reform in Nashville last week — yet he told aides he believed the measure could not pass without a push from him.

By Thursday afternoon, just hours before a scheduled vote, it had become clear that his efforts — along with those of Vice President Mike Pence and other senior White House officials — had fallen short. At a meeting in Mr. Ryan’s office in the Capitol with members of the recalcitrant Freedom Caucus, top White House officials laid out the changes they had made at the group’s behest, including stripping it of federal standards for benefits that must be provided in health insurance policies, including maternity and wellness care.

Caucus members began outlining still more changes they needed to see before they could support the bill, angering Mr. Ryan and Mr. Trump’s aides. Stephen K. Bannon, the president’s chief strategist, and Mick Mulvaney, his budget director, told the group that the White House was finished negotiating and that the president wanted to know its position on the bill — yes or no.