Kazakstan's Alexey Poltoranin won the sixth leg in a sprint finish, clocking 39 minutes 1.5 seconds to finish just 0.01 seconds ahead of Valjas. Harvey crossed in third just 0.4 seconds back.
Three-time champion Dario Cologna of Switzerland surged into the overall Tour de Ski lead after previous leader Petter Northug Jr. of Norway struggled in the 15-kilometre classical mass start race.
Cologna finished 11th, 10.7 seconds behind Poltoranin, while Northug was 28th, 29.3 seconds back.
"I was in 20th most of the way until the third lap where I slowly started picking up spots," said Valjas. "I made a real effort when I was in about 10th coming up that final hill. I just stuck to the skis in front of me, and was able to sling shot down into the finish.
"It is unbelievable. I just put my head down in that sprint to the finish and double poled as hard as I could."
Harvey, who has had success on the challenging Val di Fiemme course where he won silver in the same race last year, rebounded after a difficult opening lap.
"I am happy to get on the podium again. I just love this course and to finish the Tour off with a podium feels great," said Harvey, who will not start the final stage in order to save his energy for the remainder of the season. "I was a bit rusty in the first lap, but felt good the last two laps. I was second coming up that last hill and glided down into the stadium for the finish."
Cologna, meanwhile, now holds a 6.5-second lead over Alexander Legkov of Russia, who was 17th on the day, with Northug 11.5 seconds back heading into Sunday's final stage, the traditional climb up Mount Cermis.
Legkov would have been in the lead but he was penalized 15 seconds for obstructing a competitor.
It was an emotional day for Legkov, as six Russians were killed and two others seriously injured in a snowmobile accident on a nearby ski slope Friday night. Most of them were tourists.
Earlier, three-time defending champion Justyna Kowalczyk of Poland dominated the women's 10K event for her fourth stage victory in this year's race, extending her overall lead.
Kowalczyk clocked 28:12.9 to finish 33.2 ahead of Norway's Kristin Stoermer Steira and 38.5 in front of Finland's Krista Lahteenmaki.
In the overall standings, Kowalczyk now has a 2:08 lead ahead of Norway's Therese Johaug, with Steira 2:17 back in third.
The tour ends Sunday with the traditional climb up Mount Cermis in Val di Fiemme, which will also host this season's world championships from Feb. 20-March 3. Both men and women have 9K freestyle races with handicap starts.