In a moment of unexpected high drama, Republicans were stymied once again in their effort to repeal the Affordable Care Act — and they have John McCain to thank for it.

In the early morning hours Friday, the senator showed why he earned the nickname "Maverick" over his long tenure.

McCain, who was diagnosed with brain cancer and returned to Washington to advance the health care bill, turned around and bucked his party's leadership — and President Trump — by joining two moderate Republicans, two independents and every Democrat in voting against the so-called "skinny repeal" of the Affordable Care Act, also known as Obamacare.

McCain's office released a statement from the senator on his reasoning:

"From the beginning, I have believed that Obamacare should be repealed and replaced with a solution that increases competition, lowers costs, and improves care for the American people. The so-called 'skinny repeal' amendment the Senate voted on today would not accomplish those goals. While the amendment would have repealed some of Obamacare's most burdensome regulations, it offered no replacement to actually reform our health care system and deliver affordable, quality health care to our citizens. The Speaker's statement that the House would be 'willing' to go to conference does not ease my concern that this shell of a bill could be taken up and passed at any time."

McCain continued, calling on lawmakers to "return to the correct way of legislating and send the bill back to committee, hold hearings, receive input from both sides of aisle, heed the recommendations of nation's governors, and produce a bill that finally delivers affordable health care for the American people."

The Senate voted 51-49 against the legislation aimed at dismantling the Affordable Care Act.

"This is clearly a disappointing moment," Republican Senate leader Mitch McConnell of Kentucky said immediately after the bill failed. He added, "I regret that our efforts simply were not enough this time."

McConnell, who seemed to exhaust every trick in the procedural playbook to get to this point, seemed surprised and undercut by the result.

The defeat ends — for now — the health care debate in Congress. The chamber adjourned following the defeat, and there are no further Senate votes this week. In the short term, the Senate intends to move on to defense legislation and the nomination of Christopher Wray to be the next FBI director.

In a written statement from McConnell's office after the vote, he seemed to indicate a GOP-only effort on health care may be dead.

"We look forward to our colleagues on the other side suggesting what they have in mind," McConnell said in the statement.

Republican senators said there was no consensus and no plan for what comes next on health care. Sen. Ted Cruz, R-Texas, warned of potentially severe political consequences for Republicans for failing to deliver on what has been the GOP's unifying campaign pledge for the previous three elections.

"I sadly feel a great many Americans will feel betrayed," Cruz told reporters, "that they were lied to, and that sentiment will not be unjustified."

The "skinny repeal" was a pared-down version of Republican proposals to undo Obamacare with no plan for what to replace it with. It would have eliminated the individual and employer mandate and key taxes, defunded Planned Parenthood for a year and eliminated key protections of health benefits that were required under Obamacare.

The bill was deeply unpopular, but GOP leaders worked to assure members it would never become law. Instead, they wanted the Senate to pass it in order to advance the legislation to a third round of negotiations with the House to try to craft a final bill both chambers could pass.

McCain was not moved by these assurances, including a private meeting with House Speaker Paul Ryan on Thursday prior to the Senate vote. After the vote, McCain quickly left the Capitol and declined to comment to reporters.

Something seemed afoot before the vote. It was delayed. McCain was seen huddling with Democrats. Vice President Pence, who had come to the Capitol expecting to be the tiebreaking vote, personally — and unsuccessfully — lobbied McCain on the floor to try to win his vote.

With McCain's vote apparently gone, Republicans were seen trying to persuade Alaska's Lisa Murkowski to vote for the bill — to no avail. She eventually also voted no (as originally expected) along with Maine's Susan Collins.

Some GOP senators worried the measure would go back to the House, where leaders would put it on the floor, pass it and send it to Trump — who has said he would sign whatever lands on his desk when it comes to Republican-passed health care legislation.

Before the vote, at 10:43 p.m. ET, Trump was rooting them on in a tweet: "Go Republican Senators. Go!"

Afterward, it was a different story, with the president tweeting at 2:25 a.m. ET that those who voted no had "let the American people down."

It's somewhat ironic that McCain was the one to derail what seemed like a sure Trump victory (even if only a short-term one). After all, Trump's comments about the former prisoner of war were among the earliest to land the then-candidate in controversy.

"He's not a war hero," Trump said in 2015 of McCain. "He was a war hero, because he was captured. I like people who weren't captured. He's been losing so long he doesn't know how to win anymore."

Copyright 2017 NPR. To see more, visit http://www.npr.org/.