Contador attacked on the final climb of the race and Team Sky's Thomas cracked 1km from the summit but he made up 30 seconds on the descent to take the win.
Thomas joins Tom Simpson and Bradley Wiggins as British winners of the race.
Simpson was the first in 1967, while Wiggins, who was also riding for Team Sky, won in 2012 - the year he went on to win the Tour de France.
Thomas, who won the Volta ao Algarve stage race earlier in the year, said: "It's the biggest win for sure of my whole career."
It was an incredible finish to an enthralling "Race to the Sun", which had seen stage three cancelled because of heavy snow.
Tim Wellens of Belgium won the final stage on the Promenade des Anglais in Nice with Contador crossing the line second to pick up six bonus seconds.
However, 29-year-old Thomas, who was superbly helped by team-mate Sergio Henao in the final kilometre of the ascent of Col d'Eze, finished in a group just five seconds adrift of Contador to ensure he won.
"I felt good, I felt strong, in control [when Contador first attacked]," said Thomas.
"But when he went halfway up Col d'Eze, my legs went away. I thought it's going to be all over, but Sergio stayed with me.
"I had a 54 chain ring on in the descent and I needed that - I went just full gas to finally catch up in the last few kilometres."