WASHINGTON — All week long, Senate Democrats had quietly groused that Senator John McCain made a stirring return to the Senate after a brain cancer diagnosis, that he preached the virtues of bipartisanship — and that he then backed a Republican-only push to replace the Affordable Care Act.

But early Friday morning, Mr. McCain, showing little sign of his grave illness, strode onto the Senate floor as the vote was being taken to repeal it, and shocked many of his colleagues and the nation. He sought recognition from the vote counters, turned his thumb down, and said “no.” There were gasps and some applause.

He had just derailed the fevered Republican effort to undo the Obama-era health care law.

It was a stunning moment that will be long remembered in the Senate, a flash of the maverick John McCain, unafraid of going his own way despite the pleas of his fellow Republicans. In teaming up with Senators Susan Collins of Maine and Lisa Murkowski of Alaska, who had already opposed the bill, Mr. McCain made good on his earlier promise to help defeat the measure if it didn’t meet his personal test.

No amount of arm-twisting by his peers, Vice President Mike Pence or even President Trump could sway him from the decision that he had telegraphed to some Democrats and Republicans in the anxious buildup to the vote.