First-half tries by Marcell Coetzee and Charl McLeod put the hosts in control with a 17-5 lead at the interval, but an off-colour afternoon from flyhalf Patrick Lambie allowed the tourists back in the game.
Frank Halai scored two tries, adding to one from Rene Ranger, to give the Blues a three-point lead with a minute to play, before Viljoen completed a patient attacking passage to hand the coastal side victory.
The opening quarter was largely dominated by the hosts. Lambie, who landed three out of eight attempts, squandered a fairly routine fourth-minute penalty when he skewed horribly wide, while the Sharks were also denied a try by a TMO decision as the Aucklanders faced some heavy early pressure.
That resistance finally broke in the 23rd minute when Coetzee charged in on the left, following another marauding drive by the pack.
Six minutes later the Springbok flank was at it again, this time providing the assist, as his initial pick-up and drive opened up some space for McLeod to come from behind and scamper in.
That gave the Sharks a 14-0 lead, but seven minutes before the break, the Blues finally conjured up a response.
Following a good forward break, the ball was spread wide to Halai and the winger showed some powerful leg drive to force his way past two defenders and dive over in the right corner for a ninth try of the season.
Lambie then missed a second kick at goal, but he did find his range with the last kick of the half.
The gap was 12 points at the break, but that was reduced to seven straight afterwards as a kind bounce by a Piri Weepu kick-ahead fell kindly for the Blues attack, with the ball then spread cross-field to a lurking Halai who ran in on the right.
Five minutes later the New Zealanders were level after replacement Ranger picked up and sliced his way over for a sixth try of the campaign.
Lambie could have put the Sharks back in front before the hour mark but he wasted a third penalty, again won by the powerful home scrum.
Bismarck du Plessis finally made his anticipated return in the black and white after over 10 months on the sidelines due to injury, but he could do little to prevent the visitors from taking a 61st minute lead when Baden Kerr popped over a penalty.
Lambie should have drawn the scores level with 12 minutes to play, but his woeful afternoon continued.
He was saved, however, by Viljoen who collected from James and dived over the tryline.
Substitutes: 16. Bismarck du Plessis, 17. Daniel Mienie / Wiehahn Herbst, 18. Jandre Marais, 19. Jean Deysel, 20. Jacques Botes, 21. Cobus Reinach, 22. Jaco van Tonder
Blues: 15. Charles Piutau, 14. Frank Halai, 13. Jackson Willison, 12. Francis Saili, 11. George Moala, 10. Chris Noakes, 9. Piri Weepu, 8. Peter Saili, 7. Luke Braid, 6. Steven Luatua, 5. Ali Williams (captain), 4. Anthony Boric, 3. Angus Ta’avao, 2. James Parsons, 1. Tim Perry
Substitutes: 16. Keven Mealamu, 17. Ofa Tu’ungafasi, 18. Kane Barrett / Culum Retallick, 19. Brendon O’Connor, 20. Jamison Gibson-Park, 21.Baden Kerr, 22. Rene Ranger