Cameron and his party had hoped to come second or even win the parliamentary election in Eastleigh, but were pushed into third place by the UK Independence Party (UKIP), a party that advocates taking Britain out of the European Union and strongly opposes immigration.
The result, which saw the Liberal Democrat party take first place, will pile pressure on Cameron from disgruntled lawmakers within his own party who fret he may not be able to lead them to victory in a 2015 general election since, to do so, he will have to win parliamentary seats like Eastleigh.
One senior party figure and a former leadership candidate, David Davis, warned before the vote that "a crisis" would ensue if the Conservatives were beaten into third place by UKIP.
Farage, UKIP's leader and a member of the European Parliament, said the result showed Britons were weary of mainstream politics.
"It just goes to show that the UKIP message is really really resonating with voters," he told Reuters. "The EU and immigration are the same debate and that's the message the British public are now beginning to understand."
A boost for troubled Liberal Democrats
The result was an important symbolic victory for the Lib Dems, however, who have been hit by a sex and perjury scandal, and for Nick Clegg, the deputy prime minister and party leader, who has seen his leadership come under pressure in recent weeks.
The Lib Dems polled 13,342 votes, UKIP 11,571 votes, the Conservatives 10,559 votes and the Labour party 4,088 votes. That means UKIP took almost 28% of the vote, one of their best results in a British parliamentary election to date.
Grant Shapps, the Conservative party chairman, denied the outcome was "a crisis" for Cameron, saying it was virtually unheard of for a governing party to win a new seat mid-term.
Diane James, UKIP's candidate, said the result was "a humongous political shock."
UKIP has siphoned off support from Cameron's Conservatives by attacking EU bureaucracy and immigration from eastern Europe, tapping into what he says is a widespread feeling that mainstream parties have ignored voters' concerns.
The Eastleigh vote does not truly reflect national sentiment - the opposition Labour party leads in the polls nationally with the Liberal Democrats trailing in third or fourth place.
The election came at a time when Clegg's leadership of his party, without whom the rightist Conservatives cannot govern or pass legislation, faces intense pressure.
A sex scandal that has ensnared Chris Rennard, his party's former chief executive, has raised difficult questions for Clegg about what he knew about the imbroglio and when and how he dealt with it, amid accusations of a cover-up. Rennard strongly denies the accusations.