The teenager improved on her previous best free skate score by 18 points to clinch gold, as Kim took silver and Italy's Carolina Kostner won the bronze.
Kim had been bidding to become just the third woman in history to win back-to-back titles after Norway's Sonja Henie, a triple winner between 1928 and 1936, and Germany's Katarina Witt in 1984 and 1988.
But it was four-time Russian national champion Sotnikova - runner-up at the Europeans behind her compatriot Julia Lipnitskaia in January - who made history, much to the delight of the locals who made up the majority of the audience at a packed Iceberg Skating Palace.
“All the sacrifices were worth it because the feelings I'm having now are difficult to describe,” said Sotnikova.
The Russian had been just 0.28 behind Kim after the previous day’s short programme, with Kostner, 27, a further 0.52 adrift.
In her free routine to "Introduction and Rondo Capriccioso" Sotnikova landed seven triples including a triple Lutz-triple toeloop to score 149.95 points for an overall total of 224.59.
“I smashed my season's best. In fact, I smashed my highest score for my whole career and I did it at the Olympics,” said the thrilled teenager.
“I didn't think I could skate like I did today. The atmosphere was wonderful. I felt something amazing coming from the crowd. I could hear shouts and screams the whole time of 'keep going Adelina', and 'you can do it'. I just couldn't skate badly,” she added.
“I found something totally different in myself today. I had a bit of nerves before I skated but just before I started I was completely calm. I just felt how much I love to skate. I think I found a new me.”
The Russian teenager overcame an error when she two-footed a double loop in her combination jump to post the second best score ever in the free skate, and she was just 0.11 off the world record mark Kim achieved on her way to gold in Vancouver four years earlier.
Kim, for whom Sochi 2014 represents her competitive swansong, was philosophical and typically gracious in defeat.
“I'm so happy to be here,” said the Korean. “That was my last competition as a skater. I did a clean short and a clean long.”
The 23-year-old, known as "Queen Yu-Na" in her native country, scored 144.19 points for her free skate to Astor Piazzolla's tango "Adios Nonino" which included six triples, to give her an overall score of 219.11.
Meanwhile Kostner's stunning skate to Ravel's "Bolero" included seven triples and she achieved 142.61 for her skate for an overall 216.73 to give Italy their first ever Olympic singles medal in the figure skating.
Sotnikova’s victory meant a third figure skating gold for Russia at Sochi 2014, following the success of Tatiana Voloshozhar and Maxim Trankov in the pairs, and their triumph in the inaugural team event.