The Partenopei travelled to the Rasunda Stadium knowing that victory - coupled with defeat for PSV Eindhoven against Dnipro - would see them claim a place among the last 32.
And that is exactly what happened, with Dnipro also winning 2-1 in Holland.
Blerim Dzemaili set Napoli on their way in the 19th minute but Helgi Danielsson headed home the equaliser 16 minutes later.
A fractious second half saw Salvatore Aronica dismissed for a professional foul five minutes from time but Cavani stepped up to clinch qualification from the penalty spot three minutes into stoppage time.
The Swedish club's scouts will have filed extensive reports on Cavani after the Uruguayan hit four goals past Dnipro last time out, but he turned provider for the first goal of the evening.
Napoli pressed the ball hard from a throw-in deep inside AIK's half, Cavani seizing control and finding Dzemaili from the left wing.
The Swiss midfielder volleyed home and Napoli looked good for a place in the last 32.
AIK reacted well, though, and Napoli would soon surrender their narrow advantage.
With 35 minutes played, Viktor Lundberg produced the killer ball that Danielsson met for the equalising header.
It took a near miss from Bangura to liven things up after the break.
Lundberg gave Antonio Rosati a rare feel of the ball with a drilled shot from distance on the hour mark but chances were at a premium.
The deadlock did not suit Cavani and he took it upon himself to open the game up in the final 20 minutes.
Ivan Turina was lucky to get to the Uruguayan's fierce stab from six yards before Dzemaili smashed the ball against the Solna stopper, who parried expertly.
Bangura, the Swedes' bright spark throughout, wriggled clear to shave the crossbar with a speculative drive.
At this point Dnipro had taken the lead in Eindhoven but Cavani's weak effort six minutes from time was not enough to take the Italians through.
Aronica then made qualification an even taller order by hacking Bangura down and seeing red, and soon AIK began to pour forward in search of a last-gasp winner
But when Per Karlsson conceded a penalty deep into stoppage time with a foul on Cavani, his victim stepped up to wrap up qualification with a game to spare.