Alex Cole-Hamilton, who has held the seat since 2016, received 25,578 votes.
The seat was an SNP target, needing just a 3.73 swing to gain it, but candidate Sarah Masson trailed behind with 15,693 votes - leaving Mr Cole-Hamilton with a majority of 9,885.
In third place, the Conservative candidate and Pentland Hills councillor Susan Webber received 2,798 votes, while the Labour Party candidate, Margaret Graham, received 2,515 votes.
Daniel Fraser, of the Libertarian Party, came last with 201 votes.
As part of his victory speech, Mr Cole-Hamilton paid tribute to the SNP’s Ms Masson, saying: "Sarah campaigns with a decency and a kind heartedness that is seldom seen in politics."
He also said: “To my constituents, I say this: you are my first consideration in all things and whether you voted for me or not I will fight for you and I will never let you down.
“When an opinion poll, for an entire year, shows you losing your seat, holding your nerve becomes a way of life.
“I am entirely grateful to those individuals who have shared that way of life with me this past 12 months - without you I would not be standing here now.
“I am not sure I will ever be able to repay the debt of gratitude I owe you. My final thanks must be directed towards my amazing wife.
“Jill, your love is the rock on which all of this is built. Thank you for sustaining me every step of the way.”
Speaking after her loss, Ms Masson said: “I think the collapse in Tory support played the biggest part here - they nearly didn’t get their deposit back - that led the way for the Lib Dems.
“It’s also the most votes that the SNP has ever gotten here in Edinburgh Western.
“I’m particularly proud that right across the country, that the SNP is increasing its vote, and more women are getting elected - that’s something people told me on the doorstep, that it’s inspiring to see young women standing for office.
“It’s amazing watching so many of them across the country get into the Scottish parliament.”
A message from the Editor:
Thank you for reading this article. We're more reliant on your support than ever as the shift in consumer habits brought about by coronavirus impacts our advertisers.
If you haven't already, please consider supporting our trusted, fact-checked journalism by taking out a digital subscription.