The Scottish National Party’s (SNP) Stephen Gethins has been re-elected as the MP for North East Fife, winning by just two votes.
Mr Gethins won 13,743 votes compared with Elizabeth Riches of the Liberal Democrats, who won 13,741.
Mr Gethins, the SNP’s spokesperon on Europe, now has the smallest majority in parliament, down from 4,344 at the 2015 election.
It is the joint-third narrowest victory in UK general election history and the closest since the Lib Dem’s Mark Oaten won Winchester by two in 1997.
The result followed three recounts in total, with Mr Gethins hailing a “clear decision from the returning officer.”
Tony Miklinski won 10,088 votes for the Conservative party, while Rosalind Garton of the Labour party polled 4,026. The independent candidate, Mike Scott-Hayward, came fifth with 224 votes.
Turnout for the constituency was 41,822 (71.3 per cent).
The result came at the end of a crushing night for the SNP, losing 22 seats in total, including Alex Salmond’s constituency in Gordon.
The national result also resulted in losses for the Conservatives overall, leaving them as the largest party in a hung parliament.