The Senate passed legislation to overhaul the tax code early Saturday morning, handing Republicans a badly needed legislative and political victory.

Senators voted 51-49 to pass the plan, capping off days of debate and hand-wringing as leadership worked frantically behind the scenes to win over holdouts and get the proposal in line with the chamber's rules.

Republican Sen. Bob Corker (R-Tenn.) bucked party leadership and voted against the plan amid lingering concerns over the impact on the deficit. No Democratic senator supported the legislation, with Democrats quickly leaving the chamber after voting "no."

Vice President Pence presided over the final passage vote. GOP senators, who stayed on the Senate floor until the vote closed, broke out into applause after Pence announced the bill had passed.

"This is a great day for the country," Majority Leader Mitch McConnell (R-Ky.) said during a 2 a.m. press conference after the vote.

"We have an opportunity now to make America more competitive, to keep jobs from being shipped offshore and to provide substantial relief for the middle class."

Corker, who is retiring after 2018, said in a statement ahead of the vote that he "wanted to get to yes" on the tax plan.

"But at the end of the day, I am not able to cast aside my fiscal concerns and vote for legislation that I believe, based on the information I currently have, could deepen the debt burden on future generations," he said.

The bill would lower tax rates for individuals through 2025 and permanently cut the corporate tax rate from 35 percent to 20 percent. The bill's tax cuts for individuals are temporary in order to comply with budget rules that the measure can't add to the deficit after 10 years.

The bill would also repeal ObamaCare's individual mandate, a priority for President Trump and many Republicans, and open up a section of the Arctic National Wildlife Refuge for drilling.

Republican senators will now move to reconcile their legislation with the House's proposal, passed in mid-November, as they try to get a final product to Trump's desk by the end of the year.

The vote in the early morning hours of Saturday came after Trump met this week with key senators, including leadership and holdouts. Marc Short, the White House director of legislative affairs, was also spotted outside of the Senate chamber in the hours leading up to Saturday's vote.

McConnell had a narrow path to getting the bill through the upper chamber. With a 52-seat majority, he could only afford to lose two GOP senators and still let Pence break a tie.

But GOP leadership appeared confident on Friday that they would be able to pass the legislation after GOP Sens. Ron Johnson (Wis.) and Steve Daines (Mont.) came on board.

Johnson and Daines had been pressuring for a larger tax deduction for small- and mid-sized businesses known as "pass-throughs."

Johnson noted that Republicans had been able to get the deduction increased from 17.4 percent to 23 percent and that he would be involved in further pass-through discussions as lawmakers work to get the bill to Trump's desk.

"A seat at the table. Not just input. Not just consulting, but a seat at the table," Johnson said when asked what leadership promised him in exchange for voting "yes."

Passage of the tax plan became clear earlier Friday when Sen. Jeff Flake (R-Ariz.) gave leadership their 50th vote for the plan.

The Arizona Republican, who is retiring after 2018, said that, in addition to getting rid of a "budget gimmick" relating to the full expensing of capital investments, he had also gotten a commitment on the Deferred Action for Childhood Arrivals program.

Sen. Susan Collins (R-Maine), a moderate who voted against the GOP's ObamaCare repeal efforts, also voted in favor of the legislation after getting several of her amendments into the bill, including the restoration of a $10,000 deduction for property taxes and a lower threshold for deducting medical expenses.

"I will cast my vote in support of the Senate tax reform bill. As revised, this bill will provide much-needed tax relief and simplification for lower- and middle-income families, while spurring the creation of good jobs and greater economic growth," Collins said.

To pay for the last-minute changes, Republicans made adjustments to the bill, including restoring the alternative minimum tax, but raising the exemption amounts, and increasing rates for the repatriation of foreign earnings currently held overseas.

But passage of the Senate legislation wasn't without drama. Republicans haggled over the details of the bill and key provisions remained unfinished in the hours leading up to the final passage vote.

And the bill seemed to be on the brink of defeat after deficit hawks, led by Corker, appeared ready to send the legislation back to the Finance Committee as they tried to get a promise that the legislation wouldn't increase the deficit.

McConnell defended the legislation in the early-morning press conference with GOP senators, saying he was "totally confident" the bill would not add to the deficit.

"I think it's going to be a revenue producer," he said.

Corker, Flake and Sen. James Lankford (R-Okla.) had pushed for a "trigger" that would spark automatic tax increases if the legislation didn't meet economic growth forecasts. But they were told by the parliamentarian that it didn't comply with the rules.

GOP leadership initially offered to include automatic tax increases, which sparked near immediate backlash from across the caucus.

"It would have been counterproductive to the central objective of this legislation, which is bringing back jobs and economic growth," Sen. Ted Cruz (R-Texas) said. "And after extended discussion with a number of senators a consensus emerged that we shouldn't be raising taxes."

Sen. Pat Roberts (R-Kan.), asked about the plan, added that his colleagues should "just quit fooling around."

In the end, Republicans dropped the tax hikes, arguing they were confident the legislation would boost the economy.

That's despite the fact that the Joint Committee on Taxation said the version of the bill that passed the Finance Committee last month would cost about $1 trillion in its first decade after accounting for economic growth.

"I think most of us have made it pretty clear that just on looking at the analysis that was done in the most recent scoring we just think they're way off in their analysis," said Sen. Mike Rounds (R-S.D.). "It just doesn't add up to us."

Democrats warned that the bill would blow a hole in the deficit, and mostly benefit wealthy individuals and businesses at the expense of the middle class.

"All of the claims that tax cuts for the wealthy and corporations will pay for themselves were not correct. It's time for my Republican friends to admit their error and come clean with the American people," said Senate Minority Leader Charles Schumer (D-N.Y.).

Sen. Chris Murphy (D-Conn.) tweeted out an image of a unicorn, writing: "Unicorns are real. Tupac is still alive. Borrowing money to pay for tax cuts for the rich shrinks the deficit."

Democrats used the freewheeling floor drama, known as vote-a-rama, to force Republicans to take politically tough votes even though it was clear the legislation would pass. They also criticized what they called a lack of transparency, with GOP leaders releasing amended legislation hours before the vote.

McConnell hit back at Democrats, arguing they were complaining about the process because they lost the fight over the tax legislation.

"This was done through regular order. The Democrats had plenty of notice. ... You complain about process when you're losing and that's what you heard on the floor tonight," he said after the vote.

Still, a Democratic amendment did pass striking a provision that would have allowed colleges that don't take federal funding to be exempt from an endowment tax. Democrats argued that the provision would have primarily benefited Hillsdale College, a conservative college in Michigan.

Senators also rejected a push by GOP Sens. Marco Rubio (Fla.) and Mike Lee (Utah) to make the child tax credit refundable to payroll taxes, and pay for it by raising the bill's corporate tax rate from 20 percent to 20.94 percent.

But an amendment from Cruz to allow taxpayers to use funds from 529 plans for K-12 education, including home schooling, was added to the bill after Pence was called in to break a tie.

The House and Senate are expected to work out the differences in a conference committee in the coming weeks.

The bills differ in a number of ways. The House bill has four individual tax brackets with a top rate of 39.6 percent, while the Senate bill has seven brackets with a top rate of 38.5 percent. The corporate rate cut takes effect in 2018 in the House bill, but takes effect in 2019 in the Senate bill.

Other differences relate to the child tax credit, the estate tax, pass-through businesses and international tax provisions.

Updated: 2:25 a.m.