The Liberal Democrats have delivered a major blow to Labour by winning the Dunfermline and West Fife by-election.
Willie Rennie overturned a huge Labour majority in a neighbouring constituency to Chancellor Gordon Brown's.
Returning officer Douglas Sinclair declared that Mr Rennie had secured 12,391 of the votes.
Labour's Catherine Stihler received 10,591 votes. The SNP's Douglas Chapman was third with 7,261 votes and Tory Carrie Ruxton secured 2,702 votes.
The seat was made vacant following the death of Labour's Rachel Squire in January.
Willie Rennie (Lib Dem) 12,391
Catherine Stihler (Lab) 10,591
Douglas Chapman (SNP) 7,261
Carrie Ruxton (Cons) 2,702
John McAllion (SSP) 537
James Hargreaves (SCP) 411
Thomas Minogue (AFBTP) 374
Ian Borland (UKIP) 208
Dick Rodgers (CG) 103
At last May's General Election, Ms Squire won the seat for Labour with a majority of more than 11,500.
That was radically overturned by Mr Rennie who secured a 1,800 majority on a 16.24% swing. The turnout was 34,578 voters or 48.69% of the electorate - down by 11.21% on the General Election.
The former chief executive of the Scottish Liberal Democrats was elated following the declaration of his victory at the Queen Anne High School in Dunfermline early on Friday morning.
Thanking the voters who backed him, he said: "Tonight they have sent a powerful message to the Labour government that will rock the foundations of Downing Street, Number 10 and Number 11.
"Labour has taken the people up and down the country for granted for far too long, too much spin and not enough delivery - it is time that Tony Blair and Gordon Brown both got that message."
Turnout was down on the General Election last year
Mr Rennie said that the Conservatives could be seen to be irrelevant in much of Britain and that it must be a "deeply depressing" night for the SNP.
Acting Liberal Democrats leader Sir Menzies Campbell said the victory had been won because of a "very good local candidate" who had fought a "very good campaign".
"In addition to that, we had no trouble persuading Liberal Democrats to come from all over the United Kingdom to give help and assistance," he told BBC News.
"I think they saw that this was an opportunity to put the difficulty of the last few weeks behind us."
The by-election had been hard-fought and had seen interventions by political big beasts including Mr Brown, who holds the neighbouring seat of Kirkcaldy and Cowdenbeath.
Mr Brown had campaigned on local issues, including controversy over tolls on the Forth Road Bridge.
The victory was likely to be all the sweeter for the Liberal Democrats, currently in the middle of a leadership battle after the resignation of Charles Kennedy.
Mr Kennedy, who quit after admitting to a drink problem, had joined Mr Rennie on the campaign trail.
Responding to the result, he said: "This is a fantastic victory for the Liberal Democrats and a seismic event in Scottish and UK politics."
Scottish Lib Dem leader Nicol Stephen said: "People are getting a positive message from the Liberal Democrats and they are fed up with Labour.
"They feel Labour has neglected the area and we campaigned positively on the key issues."
Defeated Labour candidate Ms Stihler, a Scottish MEP, paid tribute to Ms Squire and said: "This is not a result which Rachel would have wanted but I think I know what she would have said.
"We have to listen to the people and we have to learn."
The Lib Dems increased their share of the vote on the last poll by 15.67% (35.83% share), while Labour's fell by 16.81% (30.63%). The SNP saw a slight increase of 2.07% (21%), but the Tories were down by 2.51% (7.81%).
Scottish National Party leader Alex Salmond was in upbeat mood, despite the party's third place.
He said: "This has been a strong SNP performance, with the swing varying dramatically across the various areas of the seat. I would like to congratulate Douglas and his campaign team on a fine performance."
The Scottish Tories were in fourth place, despite campaigning support from new leader David Cameron.