With the win, Russia will meet the Czech Republic in quarter-final action while Sweden will face Slovakia. Malkin scored on a slapshot from the high slot at 9:51 of the third period, beating Sweden goalie Erik Ersberg low on the stick side to put Russia ahead 3-2.
Alexei Morozov cemented the win at 16:33 when he took a pass from Sergei Zinoviev and fired a wrist shot past Ersberg.
Alexander Steen tied the game 2-2, picking up a loose puck in close and slipping it between the pads of Russian goalie Alexander Eremenko at 18:22 of the second.
Denis Grebeshkov gave Russia a 2-1 lead three minutes into the period.
Sweden took a 1-0 lead at 8:43 of the first when Martin Thornberg beat Eremenko with a low shot on the power play.
Russia tied it in the 17th minute when Alexander Kharitonov split two Swedish defenders and was hauled down, giving Russia a penalty shot.
Alexei Morozov skated in and put a backhand shot over Ersberg's shoulder.
In other action, Jens Nielsen made sure Denmark ended the world hockey championship on a positive note.
Nielsen had a goal and an assist as Denmark defeated Italy 5-2 in the final tournament game for both teams.
Nielsen took a pass from Mads Christensen in the slot and fired a wrist shot past Italian goalie Andrea Carpano to give Denmark a 3-0 lead at 6:30 of the first.
Meanwhile in Mytischi, Russia, Michael Wolf scored twice, including the winning goal, to lead Germany past Belarus 6-5 and improve his team's chances of getting an automatic berth for the 2010 Olympics.
Bo Nordby Tranholm and Peter Regin also scored in the first for Denmark before Italy rallied to cut the deficit to 3-2 in the second on goals by Giorgio de Bettin and Roland Ramoser.
"We jumped out to an early lead and then let them back in it," Denmark forward Kim Staal said. "We talked about tightening our defence during the break and were able to come out in the third and play much better."
Rasmus Olsen put Denmark ahead 4-2 when he deflected Thor Dresler's pass at 9:04 of the third. With 4:31 remaining, Christensen cemented the win after converting a pass from Nielsen.
With the win, Denmark secured its first top-10 finish since joining the top division in 2003. Their final ranking is 10th. The Italians finish 12th.
Germany, promoted from Division I last year, leaves the championship in ninth place, while Belarus is 11th.
The Germans toppled the Czechs 2-0 in the qualifying stage and narrowly lost to Canada 3-2 in the preliminary round.
"This was important for Olympic qualifying. Next year we can attack eighth place for sure," Germany forward Petr Fical said. "It would be great. I think we can do it for sure."
The championship counts toward Olympic qualification in Vancouver. Nations have only this tournament and the 2008 worlds in Canada to secure an automatic berth for the Vancouver Games.
The top nine teams in the IIHF rankings after the 2008 championship will directly enter the Olympics. Those ranked 10th and under will have to fight over the last three spots in qualifying tournaments.
In the relegation round, Austria beat Ukraine 8-4. The result, however, was meaningless because both will be relegated due to Norway's 7-4 win over Latvia.
Norway and Latvia will remain in the elite 16-team division for another season.