The Scottish National Party´s, SNP, former leader Alex Salmond has lost his Gordon seat amid a Scottish Conservative surge. The Tories also defeated the SNP's deputy leader Angus Robertson as they won seats across the country. The SNP finished on 35 seats - a majority of the 59 Scottish constituencies but a drop of 21 from the 56 they won in 2015.
The Conservatives won 13 seats in Scotland, the party's best performance in the country since 1983.
Labour won seven seats and the Liberal Democrats four. The three pro-UK parties had won just one seat each the last time around. The Tories saw big increases in their vote across the country, with the SNP vote dropping sharply.
The final Scottish seat to declare was Fife North East, where there were three recounts before Stephen Gethins of the SNP was declared the winner after finishing just two votes ahead of the Lib Dem candidate.
With all 59 seats declared, the SNP won 36.9% of the votes, a drop of 13.1 percentage points since 2015. The Scottish Tories doubled their share of the vote to 28.6%, with Labour up by 2.8 points at 27.1% and the Lib Dems down slightly to 6.8%.
But the picture is less positive for the Conservatives across the UK, with the party losing its majority at Westminster and the prime minister facing calls for her resignation.
Mr Salmond, who was Scotland's first minister between 2007 and 2014, had won a majority of 8,687 at the last election in 2015. But the Conservatives saw their share of the vote increase by 29% in the constituency as Colin Clark defeated the former first minister by 2,607 votes.
Despite his defeat, Mr Salmond insisted that people have not seen the last of my bonnets and me. He added: The SNP have lost many fine parliamentarians this evening and that's a grievous blow to the SNP.
But overall the result in Scotland shows the SNP will have won a majority of the seats in this country and the majority of the vote, something which I suspect the prime minister would like to be able to claim in the early hours of this morning but is not able to do so.
Earlier, Douglas Ross of the Tories overturned a majority of 9,065 to defeat Mr Robertson - who had been the SNP's leader at Westminster - by 4,200 votes in Moray.
Speaking as she arrived at the Glasgow count, First Minister Nicola Sturgeon said that the SNP had won the election in Scotland, with the party achieving its second best performance ever in a Westminster election.
Asked about the impact of the lost SNP seats on her desire to hold a second independence referendum, Ms Sturgeon said: Like most politicians I have not had any sleep and I am not going to take any rash decisions.