The Liberal Democrats have held on to the Eastleigh constituency in Hampshire.
The party has won the by-election triggered by Chris Huhne's resignation - but with a reduced majority.
UKIP made big gains, finishing second ahead of the Tories in what the party described as a "humungous political shock."
The Liberal Democrats held off a late Ukip surge to win the by-election, but David Cameron was dealt a serious blow by the Conservatives' third place.
Nick Clegg said his party was “on track” for the 2015 general election after local councillor Mike Thornton held on to the seat vacated by disgraced ex-cabinet minister Huhne.
He won with 13,342 votes, a majority of 1,771 over the UK Independence Party’s Diane James, who said beating the Tories was a “humongous” shock that represented a “seismic shift” in UK politics.
Tory Maria Hutchings polled 10,559 votes – more than 1,000 behind Ukip, which snatched huge chunks of the coalition parties’ 2010 general election vote share, taking more than 27% of the total.
Conservative Party chairman Grant Shapps tried to play down the significance of the shock result – which influential backbench MP David Davis had warned would mean a “crisis” for his party.
Mr Shapps insisted you “can’t read much into by-elections” – seeking to explain the poor showing in what had initially been billed as a two-way fight between the coalition partners as a classic mid-term voter revolt.
But the failure not only to overcome a Lib Dem party engulfed in the Huhne scandal and claims of sexual harassment by a senior party figure but also to outperform Ukip will reignite smouldering backbench dissent.