Democrats said Americans could “breathe a sigh of relief” after seven years of non-stop railing against former president Barack Obama’s Affordable Care Act health care law.
But Mr Trump said Obamacare was imploding “and soon will explode”.
Thwarted by two factions of fellow Republicans, from the centre and far right, House speaker Paul Ryan said Mr Obama’s health care law, the Republicans’ top target in the new Trump administration, would remain in place “for the foreseeable future”.
It was a stunning defeat for the new president after he had demanded House Republicans delay no longer and vote on the legislation on Friday, pass or fail.
But his gamble failed and instead Mr Trump, who campaigned as a master deal-maker and claimed that he alone could fix America’s health care system, saw his ultimatum rejected by Republican politicians who made clear they answer to their own voters, not to the president.
At the White House, a dejected but still combative Mr Trump said he had “never said repeal and replace it in 64 days”, though he had repeatedly shouted during the presidential campaign that it was going down “immediately”.
The bill was withdrawn just minutes before the House vote was to take place and politicians said there were no plans to revisit the issue.
Republicans will try to move ahead on other agenda items, including overhauling the tax code, though the failure on the health bill can only make whatever comes next immeasurably harder.
Mr Trump pinned the blame on Democrats, saying: “With no Democrat support we couldn’t quite get there.
“We learned about loyalty, we learned a lot about the vote-getting process.”
The Obama law was approved in 2010 with no Republican votes.
Despite reports of backbiting from administration officials toward Mr Ryan, Mr Trump said: “I like Speaker Ryan. I think Paul really worked hard.”
For his part, Mr Ryan told reporters: “We came really close today but we came up short. This is a disappointing day for us.”
He said Mr Trump had “really been fantastic”, but when asked how Republicans could face voters after their failure to make good on years of promises, he quietly said: “It’s a really good question. I wish I had a better answer for you.”
In the autumn, Republicans used the issue to gain and keep control of the White House, Senate and House of Representatives.
During previous years, they cast dozens of votes to repeal Mr Obama’s law in full or in part, but when they finally got the chance to pass a repeal version that actually had a chance to become law, they could not deliver.
Democrats could hardly contain their satisfaction.
“Today is a great day for our country, what happened on the floor is a victory for the American people,” said House minority leader Nancy Pelosi, who as speaker helped Mr Obama pass his Affordable Care Act in the first place.
“Let’s just for a moment breathe a sigh of relief for the American people.”
The outcome leaves both Mr Ryan and Mr Trump weakened politically.
For the president, this piles a big early congressional defeat on to the continuing inquiries into his presidential campaign’s Russia connections and his unfounded wiretapping allegations against Mr Obama.
Mr Ryan was not able to corral the House Freedom Caucus, the restive band of conservatives that ousted the previous speaker.
Those Republicans wanted the bill to go much further, while some moderates felt it went too far.
Instead of picking up support as Friday wore on, the bill went the in other direction, with several key politicians coming out in opposition.
Congressman Rodney Frelinghuysen of New Jersey, who chairs a major committee, Appropriations, said the bill would raise costs unacceptably on his constituents.
The Republican bill would have eliminated the Obama statute’s unpopular fines on people who do not obtain coverage and would have also removed the often-generous subsidies for those who bought insurance.
Republican tax credits would have been based on age, not income like Mr Obama’s, and the tax increases Mr Obama imposed on higher-earning people and health care companies would have been repealed.
The bill would have ended Mr Obama’s Medicaid expansion and trimmed future government financing for the national programme, letting individual states impose work requirements on some of the 70 million beneficiaries.