A candidate who was suspended from the SNP for using anti-Semitic language on social media has won his seat.
Neale Hanvey still appeared on the ballot paper as the party's candidate for Kirkcaldy and Cowdenbeath because his suspension came too late for changes to be made.
He will sit as an independent MP until a disciplinary process is completed.
Mr Hanvey was elected with a majority of 1,243 - unseating Labour's Lesley Laird, the shadow Scottish secretary.
His election is thought to be the first time a candidate who was dropped by their party has been elected as an independent.
Ms Laird got 15,325 votes to Mr Hanvey's 16,568. She won the seat in 2017 by just 259 votes.
The Scottish Conservatives' Kathleen Leslie received 9,449 votes.
Gill Cole-Hamilton, for the Scottish Liberal Democrats, got 2,903.
Scott Rutherford for the Scottish Greens was on 1,628 and Mitch William for the Brexit Party won 1,132.
What was the reaction to the result?
Mr Hanvey said he hoped to do a "really good job".
"I don't feel triumphant in any sense, it's more about a sense of responsibility," he said.
He added he had not expected people to cheer for him following the declaration.
"I've been suspended from the party and people have to take that seriously until it's resolved. So I don't expect them to be whooping and wailing for me, I think that's part of the work I have to do to remedy the situation."
Lesley Laird said she was "really disappointed" to lose her seat.
She said the "fundamental issues" had not been resolved in the election, adding: "In the middle of a constitutional wrangle, I believe it's very important that we don't lose sight of our core value and principles just to be popular.
"Because constitutional politics are not good. And we can see that history tells us that in the long run."
She said it was now for the SNP to continue with their disciplinary issues and to address the "very serious" issues that resulted in Mr Hanvey's suspension.
On what has been a bad night for Scottish Labour, this is possibly the ultimate embarrassment - the shadow Scottish secretary losing her seat to a man disowned by the SNP.
Neale Hanvey may have been suspended, but he was still listed as the SNP candidate on the ballot and it goes down as their gain on paper.
Despite the support of many local independence activists, he actually garnered fewer votes than the SNP did in the seat in 2017 - but Lesley Laird managed to shed even more. In what was a strange race all round, the top three parties all lost votes despite turnout being up.
Mr Hanvey will initially sit in the Commons as an independent MP, albeit one who will vote very much in line with the SNP - but he will later go through a disciplinary process to see if his party membership will be reinstated.
Why was Neale Hanvey suspended from the SNP?
Mr Hanvey, a former leader of the SNP group on Fife council, was selected to contest the seat - but was later suspended by the party after scrutiny of posts he had previously shared on Facebook.
The SNP said it had withdrawn all support for his campaign after consultation with Jewish community leaders.
His suspension was a result of two social media posts:
- One contained a link which included a cartoon of billionaire George Soros as a puppet master controlling world leaders.
- Another drew parallels between the contemporary treatment of Palestinians and the "unconscionable treatment" of Jews in World War Two.
Mr Hanvey apologised in a tweet, saying he "did not give any thought to Mr Soros' faith and did not consider the connotations of the image in that context. I fully accept that was wrong and I apologise unreservedly for any offence caused".
He also apologised for the post in comparing the treatment of Palestinians to that of Jews in World War Two.
Mr Hanvey added that he fully supported the decision to suspend him and said Nicola Sturgeon was right to take a zero-tolerance approach.
Mr Hanvey later told BBC Scotland that "of course" he would like to be readmitted to the party, saying: "The SNP is an incredibly important part of my life."
A member of the party's conduct committee who was expected to investigate Mr Hanvey's case resigned from the SNP after she was also accused of anti-Semitism.
A brief history of the Kirkcaldy and Cowdenbeath constituency
The Fife seat was held - with a majority of over 50% - by former Prime Minister Gordon Brown, who held it from its creation in 2005 until the 2015 election, which he did not contest.
It was then taken by the SNP's Roger Mullin.
However, the seat went back to Labour - to Lesley Laird - in the 2017 snap election.
Will Mr Hanvey be an SNP MP?
Because Mr Hanvey was suspended after nominations closed, his name still appeared on the ballot paper as the SNP candidate.
Because Mr Hanvey was listed on the ballot as an SNP candidate, when the Kirkcaldy and Cowdenbeath result was delivered it was declared as a victory for the SNP.
However, as Mr Hanvey has been suspended from the SNP he will initially sit as an independent MP until disciplinary proceedings are completed.
Once this happens, he could be re-admitted into the party and sit as an SNP MP.
For a nationwide breakdown of results, see our results page, which will be updated throughout the night.
Or you can browse the A-Z list.