Is it actually even possible to beat the Lib Dems in a by-election? The incumbent resigned and faces jail, the party's national poll rating is in the gutter and the week leading up to Thursday's vote was dominated by an alleged cover-up of a sex-scandal, but Nick Clegg's party has still managed to swing a win in Eastleigh.

Mike Thornton, a local Lib Dem councillor, was elected with 13,342 votes to replace Chris Huhne in parliament, a majority of 1,771. But the story of the night was who came second. Ukip candidate Diane James secured 11,571 votes, beating Tory Maria Hutchings into third place on 10,559, the result of David Cameron’s nightmares.

The by-election, which was the first time the coalition parties went head-to-head since 2010, was seen as a must win for both Clegg and David Cameron, with both leaders desperate to show their parties they were not condemned to lose in 2015.

Given the circumstances of the by-election the prime minister could have been forgiven for having been quietly confident of wresting the Lib Dem-Tory marginal away from his coalition partners – or at the very least coming a strong second.

But a surge in support for Ukip, charted by the party’s leader Nigel Farage in a series of blog posts on The Huffington Post UK during the last days of the campaign, will lead many Tory MPs to question Cameron’s ability to deliver a majority in 2015.

On Wednesday the senior Tory who challenged Cameron for the leadership of the party in 2005, David Davis, warned that losing to Ukip would prompt a "crisis" for the prime minister

In Hutchings the Tories had a candidate who would have been at home within Ukip, sharing as she did several of their policy positions including favouring withdrawal from the EU and opposition to gay marriage.

The fact Hutchings, dubbed the Tory ‘Sarah Palin’ by critics, was far from a Cameron moderniser signals how worried the Conservative Party is by the threat Ukip poses from the right.

The result will also lead to concerns that Cameron’s recent promise to hold an in/out referendum on the EU if he wins re-election has not been enough to kill-off the Ukip threat.

The Tories flooded Eastleigh with cabinet ministers and MPs during the three-week campaign in a failed attempt to unseat the Lib Dems and see off Farage.

Leaflets distributed by the party made much of how the region's Ukip MEP had recently defected to the Tories and endorsed Hutchings.

And on the morning of polling day, Cameron tweeted a short message that highlighted welfare, immigration and the deficit as key issues - hardly the modernising message of 2010 that focused on the environment and the Big Society.

For Cameron to secure a majority at the next election he needs to increase his vote share and number of MPs – a rare achievement for a popular incumbent government, let alone one trailing in the national polls.

For Clegg the victory will solidify his position within the party. The deputy prime minister has endured one of his worst weeks as Lib Dem leader - and there have been many bad weeks - amid accusations of sexual harassment against the party's former chief executive Lord Rennard.

The win in Eastleigh will enable Clegg to argue entering into a coalition with the Tories has not doomed the party to electoral oblivion in 2015. Despite the party's dismal poll ratings since 2010 Lib Dem MPs and activists have remained remarkably loyal to their leader, holding Eastleigh will likely crush any leadership chatter for some time.

Speaking after the result Thornton said he won "because the people of Eastleigh recognised the Lib Dems have always had superb record of delivery".

Taking glee in the Tory failure, Lib Dem deputy leader Simon Hughes said the by-election "could not have been a more difficult defence" for his party.

Labour was never expected to poll too well in the southern seat, and its candidate John O'Farrell came fourth. Not a brilliant showing considering the party came third in 2010.

But arguably the result does bode well for Ed Miliband. If the Lib Dems are able to hold off the challenge from the Tories in the south and Cameron remains vulnerable to Ukip in marginals at the general election in 2015, the Labour leader's chances of becoming prime minister are increased.