The Liberal Democrats on Friday hung onto the southern England parliamentary seat of Eastleigh, dealing a heavy blow to Prime Minister David Cameron whose Conservative Party were beaten into third place.
The election was billed as a straight battle between the Conservatives and the Lib Dems, their junior coalition partner, but the anti-EU UK Independence Party (UKIP) capitalised on voter disillusionment to claim second place.
A senior Conservative earlier warned that third place would be a "crisis" for his party.
The returning officer announced shortly after 02:00 GMT that Lib Dem candidate Mike Thornton had secured 13,342 votes, 1,771 more than UKIP representative Diane James.
Tory nominee Maria Hutchings limped in third with 10,559 votes.
The victory handed embattled Lib Dem leader Nick Clegg a much needed boost.
The election was sparked by the resignation of disgraced former energy minister Chris Huhne, a Liberal Democrat who has pleaded guilty to trying to avoid a speeding fine.
The party has also been damaged by an ongoing sex scandal surrounding its former chief executive Chris Rennard, and the result looks set to cause ructions within an already strained coalition.
Thornton credited the party's "fighting spirit" for securing victory and paid tribute to Clegg, saying that his support and visits to the town had given a "tremendous boost" to the campaign.
"Tonight is a great night for the Liberal Democrats nationally," he added. "It sends a strong signal of support for Nick Clegg."
A statement released by the Lib Dems at close of polling said the defeat would be "a major blow" to the Tories.
"If the Conservatives can't beat us... under these circumstances, when can they beat us?" it added.
Senior Conservative David Davis earlier warned that a loss for the party would place serious doubt over Cameron's leadership of the party.
"If we came third it would be a crisis," Davis told BBC television.
Some 58 percent of the 79,000 residents eligible to vote turned out to choose the winner from the 14 candidates, who had campaigned incessantly since the election was called following Huhne's resignation on February 5.
On the eve of polling, Cameron urged Conservatives to back the party's candidate Maria Hutchings, who vowed to help "get the country back on its feet" if she won.
Clegg visited Eastleigh on Wednesday to pledge his support for Thornton, saying he was on the "cusp of a great, great victory".
Addressing supporters at Lib Dems headquarters, Clegg called the race the "most exciting and closely contested by-election" that he could remember.
UKIP's James said her second place finish was a "humongous shock" that showed the party was now a major force in British politics.
"This will be an absolutely seismic shift in terms of politics and political thinking if UKIP overtakes the Tories in such a key target seat for them," she said.
UKIP leader Nigel Farage claimed that Cameron's focus on "wind turbines and gay marriage" instead of free enterprise and business had lost him support in the party's heartlands.
"Nobody can ever again say that a UKIP vote is a wasted vote," he told Sky News. "We're getting nearer and nearer."
John O'Farrell, the candidate for the main opposition Labour party, finished fourth after failing in his bid to secure the protest vote.