The Russian pair were split by only 0.2 seconds at the halfway stage.
However, surprisingly, Danilova extended her lead, crossing the line seven seconds ahead of the field.
The 31-year-old, a classical specialist, was not expected to hold on for gold during the freestyle leg.
But she shadowed Lazutina in the freestyle, before making a break after 3.7km after which she never looked back.
Having taken the first race in a time of 12 minutes 58.7 seconds she finished the freestyle in a time of 11 minutes 54.1 seconds.
Lazutina came in 6.9 seconds later to take silver, with Canada's Beckie Scott a further 10.9 seconds back claiming bronze.
Petra Majdic of Slovenia, who had been third after the classical race, faded to ninth, while women's 10K classical gold medallist Bente Skari of Norway dropped from fourth to eighth.
"After the first race I was hoping for one of the medals - gold or silver - but I was not 100% confident which one," Danilova said after her win.
It was her second medal of the games after a silver in the 10K classical, but more significantly it meant that she denied Lazutina a record sixth Olympic gold.
"The strongest of us got the best result," Lazutina said in defeat.
"I believe as far as skiing is concerned this is the most difficult event and second place gives me immense joy.
"Ultimately, what is important is that Russia got its gold medal," the 36-year-old added.
Scott picked up Canada's first Olympic medal in the sport but only after a photo-finish with the Czech Republic's Katerina Neumannova that required numerous replays to split the athletes.
"This has been 10 years in the making and it's been a dream come true," Scott said.