Halfvarsson allowed Petter Northug and Federico Pellegrino to take the lead early on in the sprint final before attacking under the bridge and holding for victory in Germany.
Northug fell away while neither Pellegrino nor Martin Johnsrud Sundby could get the better of Halfvarsson in the lanes as he clocked a winning time of 3:08.61 minutes.
He is now second in the Tour de Ski standings, 20.2 seconds behind leader Alex Harvey who fell in the semi-finals of the sprint after winning the opening prologue stage in Oberhof.
With soft conditions and snowfall affecting the skis, Halfvarsson followed the lead of fellow Swede Hanna Erikson who performed brilliantly to win the women's sprint.
One of the slowest of the qualifiers for the knockout stages in 24th, Denise Herrmann and Erikson led into the lanes with the latter coming out on top for the victory.
She clocked 3:25.69 to win by 0.79 although fifth was enough for Marit Bjoergen to maintain her overall lead ahead of Herrmann after victory in the opening prologue.