By Hayley Jarvis
BBC Scotland news website reporter
First Minister Alex Salmond had promised another SNP by-election earthquake in Glenrothes, but it appeared it was the "Brown bounce" that shook voters in Fife, with Labour winning a 6,737 majority.
The count went Labour's way after a night of twists and turns
Before the final result was officially declared, Labour candidate Lindsay Roy entered the sports hall at Fife Institute, where the count had taken place, to loud cheers and applause.
The mild-mannered head teacher was swamped by his supporters and press photographers, though Labour hadn't always been so confident of a win.
Despite holding a 10,664 majority in the last General Election, the SNP success in Glasgow East in July proved there was no such thing as a Labour safe seat.
There were hopes that Glenrothes would be different, particularly as the former mining town borders on Prime Minister Gordon Brown's own Kirkcaldy and Cowdenbeath constituency.
Labour appeared to have narrowed the gap as the campaign wore on, perhaps in response to Gordon Brown's economic policies, or the unpopularity of the SNP council's home care charges.
But as the election came closer the might of the Nationalist campaigning machine showed no signs of abating.
More than 1,000 activists had pounded the streets of Glenrothes to drum up support for their party, but of the stream of yellow and black rosettes that filled the hall earlier in the evening only a handful greeted Peter Grant when he arrived just before 0020 GMT.
The party seemed to have already conceded defeat.
The count began with military precision just before 2210 GMT, when the first of 95 ballot boxes arrived from Rimbleton Primary School.
More black boxes arrived soon after and the hundred-plus volunteers busied themselves sorting the papers by hand.
Mr Roy said Gordon Brown was the man to lead the country
The gloomy November weather had seemingly not dampened the turnout - 52.37%, not bad for a by-election. Word had it they were only expecting 48%.
Representatives from each of the political parties surrounded the desks of counters trying to gauge their candidate's success.
By 2230 GMT there were mutterings of a Labour lead, but by 2300 GMT there were contrary murmurs that the SNP was making gains.
By 2315 GMT the returning officer, Ronnie Hinds, announced the verification process was almost complete and the next phase of counting was due to begin.
Not long after, candidates from some of the smaller parties began to appear.
HAVE YOUR SAY
For once it isn't labour spin but the truth to call this a good victory
A Brown, Edinburgh
Louise McLeary, from Solidarity, was chatting to Kris Seunarine, from the UK Independence Party - Morag Balfour was joined by the Scottish Socialist Party convener Colin Fox.
Jim Parker, from the Scottish Senior Citizens' Unity Party, was among the crowds, wearing a smiley faced rosette.
But in reality the by-election was going to be a two horse race, with the Liberal Democrats and the Conservative Party vying for third.
Harry Wills, for the Lib Dems, arrived at 2340 GMT smiling with his wife, but an even cheerier Maurice Golden, the Conservative candidate, emerged just after midnight, his expression perhaps an indication of how that fight was about to turn out.
By that time the rumour mill was spinning news of a Labour victory and the grey faces of the SNP activists seemed to confirm it.
The count finished earlier than expected - so early in fact that Mr Grant and Mr Roy had to be called and told to come in.
About 150 members of the press had gathered to hear the result with council workers remarking it was the most attention a count in their area had ever drawn.
Mr Roy was declared the winner at 0040 GMT but the result had become obvious long before the returning officer had made his announcement.
In his acceptance speech, Mr Roy paid tribute to John MacDougall, whose death in August prompted the by-election.
He said he hoped to work with Fife council leader Peter Grant and hoped he would agree to stop what he called the unpopular care home charges.
He introduced himself as a "former head teacher" and pledged to work with the prime minister, who he said was the man to lead the country in difficult economic times.
An emotional Peter Grant thanked his leader too.
The SNP has managed to slim Labour's majority in the seat, following the 2005 election.
It had hoped for more having secured the corresponding Holyrood seat and gained leadership of the local council in the three years since the last general election.
Some had assumed Glenrothes was SNP's for the losing.
But was this election an endorsement of Gordon Brown or, as Maurice Golden put it - has the SNP bubble well and truly burst?