Napoli were desperate to make up for dropping points in a 1-1 draw with Sassuolo.
Inter travelled to Naples looking to continue their strong start under new coach Stefano Pioli, who started his spell in charge with a 2-2 derby draw at AC Milan and secured a 4-2 win over Fiorentina last weekend.
But Napoli were desperate to make up for dropping points in a 1-1 draw with Sassuolo, a result that left Maurizio Sarri's men eight points behind leaders Juventus, and were soon on their way to a morale-boosting win after hitting two quick-fire goals in the opening six minutes.
A great attacking move saw Jose Callejon knock down Hamsik's pass for Piotr Zielinski, who fired a low volley from 12 yards past Samir Handanovic and into the far bottom corner after just two minutes.
The Napoli fans were gifted another gem four minutes later when Hamsik beat the offside trap on his way to a through ball and fired an unstoppable angled drive under Handanovic.
The Slovak's 104th goal for Napoli pulled him level with Cavani, who is now at Paris Saint Germain, and means he is well on his way to beating Diego Maradona's Napoli record of 115.
Napoli could have added more, Handanovic parrying Manolo Gabbiadini's strike before a header from the big forward glanced wide.
Inter spurned a chance to reduce the arrears after Pepe Reina came rushing off his line to block from Mauro Icardi with his legs.
Icardi wasted another chance when he rolled the ball across for Ivan Perisic rather than go for goal, and moments later the Argentine was denied by Reina at the near post.
But Handanovic was soon back in action, parrying a Gabbiadini snap shot and a vicious drive from Hamsik.
Inter emerged in determined fashion after the interval, but all hopes of launching a fightback were kept in check when Lorenzo Insigne volleyed a loose ball from a corner past Handanovic on 51 minutes.
Handanovic saved Inter's blushes with a point-blank save from Hamsik after Insigne's pass, while Reina denied Antonio Candreva a consolation goal with a great, one-handed save down low.
Napoli move up to sixth place at five points behind Juventus, who host Atalanta on Saturday.
Roinma, in second at four points adrift, are away to Lazio in the capital derby on Sunday when AC Milan, third also at four points behind, host Crotone.