After 14 unsuccessful rounds of voting, Republican Kevin McCarthy has been elected Speaker of the US House of Representatives.
Mr McCarthy won in the 15th round of voting after managing to sway enough hard-right members of his party.
There were dramatic scenes earlier in the late-night session, with several members involved in heated exchanges on the House floor after Mr McCarthy narrowly lost the 14th round.
For four straight days, the House had been at a standstill while the speaker's job was settled.
The 57-year-old Californian suffered one final humiliation when Representative Matt Gaetz withheld his vote on the 14th ballot, as midnight Friday approached, prompting a scuffle in which fellow Republican Mike Rogers had to be physically pulled away.
Mr McCarthy's victory in the 15th ballot brought an end to the deepest congressional dysfunction in 160 years.
However, it sharply illustrated the difficulties that the speaker will face in leading a narrow and deeply polarised majority.
He won after 15 rounds of voting on a margin of 216-211.
Mr McCarthy was able to be elected with the votes of fewer than half the House members only because five in his own party withheld their votes — not backing him as leader, but also not voting for another contender.
He agreed to a demand by hardliners that any lawmaker be able call for his removal at any time.
That will sharply cut the power he will hold when trying to pass legislation on critical issues, including funding the government, addressing the nation's looming debt ceiling and other crises that may arise.
"We got the things that are transformational," Republican Representative Ralph Norman said, who voted to back Mr McCarthy after opposing him for much of the week.
Republicans' weaker-than-expected performance in November's midterm elections left them with a narrow 222-212 majority, which has given outsized power to the right-wing hardliners who have opposed Mr McCarthy's leadership.
Several of the hardliners have questioned Mr McCarthy's willingness to engage in such brinksmanship when negotiating with President Joe Biden, whose Democrats control the Senate. They have raged in the past when Senate Republicans led by Mitch McConnell agreed to compromise deals.
The hardliners — including Freedom Caucus chairman Scott Perry and Chip Roy of Texas — said concessions they had extracted from Mr McCarthy would make it easier to pursue such tactics this year or force another vote on McCarthy's leadership if he did not live up to their expectations.
"You have changes in how we're going to spend and allocate money that are going to be historic," chairman of the hard-right House Freedom Caucus Representative Scott Perry said.
"We don't want clean debt ceilings to just go through and just keep paying the bill without some counteracting effort to control spending when the Democrats control the White House and control the Senate."
Mr McCarthy's belated victory came the day after the two-year anniversary of a January 6, 2021, attack on the US Capitol, when a violent mob stormed Congress in an attempt to overturn then-president Donald Trump's election loss.
This week's 14 failed votes marked the highest number of ballots for the speakership since 1859, in the turbulent years before the Civil War.
Mr McCarthy's last bid for speaker, in 2015, crumbled in the face of right-wing opposition. The two previous Republican speakers, John Boehner and Paul Ryan, left the job after conflict with right-wing colleagues.
However, Mr McCarthy now holds the authority to block Mr Biden's legislative agenda, force votes for Republican priorities on the economy, energy and immigration and move forward with investigations of Mr Biden, his administration and his family.
Reuters