Marek Hamsik gave hosts Napoli an early lead 10 minutes into the game before Paolo Cannavaro and a Davide Astori own goal made it 3-0 as they galloped towards a fifth straight league win.
Cagliari's Joaquin Larrivey pulled one back with the first goal of an ultimately futile hat-trick as Ezequiel Lavezzi, Walter Gargano and Christian Maggio netted for Napoli in the second half.
Inter's Walter Samuel and Diego Milito scored in the 87th and 90th minutes respectively to give Ranieri's beleaguered side some respite given their last win in all competitions was against Lazio on January 22.
The victory moved Inter up to sixth with 40 points, two more than AS Roma, who play at Palermo on Saturday (1945 GMT).
Both Napoli and Inter were playing on Friday to give them extra time to prepare for next week's Champions League last-16 second legs against Chelsea and Olympique Marseille
"Many of us scored tonight, and that is very important," Hamsik said. "Resting before the Champions League match against Chelsea will do us good."
League leaders AC Milan, who top Serie A with 54 points, host third-bottom Lecce on Sunday (1400) while Juventus, who trail Milan by two points, play at Genoa. Third-placed Lazio host Bologna and Udinese travels to Novara in late Sunday kickoffs.
Hamsik scored his first goal in two months to start the Napoli party at the San Paolo and it began to look easy as defender Paolo Cannavaro headed home nine minutes later.
Ezequiel Lavezzi's shot after 30 minutes was then deflected in by Cagliari's Astori
The first of Larrivey's goals, a header from Daniele Conti's cross, reduced the gap to 3-1 38 minutes into the first half but things looked grim for Cagliari again as soon as the teams were back for the second half.
Cagliari's Michele Canini fouled Lavezzi near the goal and the Argentine himself netted the penalty on 56 minutes before Gargano took his turn to score, curling home a shot.
Larrivey did not give up and scored his second header, only to see Napoli's Maggio make it 6-2 with a diagonal shot before the Cagliari man nodded in again on 92 minutes.
"I got quite angry because of the goals we allowed. I wasn't satisfied at all with the general distraction we displayed in parts of the game," Napoli coach Walter Mazzarri said.
"Modern soccer means you're always on your toes, no matter what stage of the game you're at, and we are not capable of keeping our attention levels up. That has to change, those goals we conceded were defensive mistakes."
Inter prevailed in a less than easy match against Chievo with two late goals after Milito had failed to score a 14-minute penalty following a handball by Chievo's Francesco Acerbi.
After Wesley Sneijder's shot hit the top of the bar, victory seemed to be escaping the Nerazzurri once again before Samuel headed in a corner.
Milito then made up for his missed penalty when he headed in a cross from the right by Javier Zanetti.
Inter coach Ranieri had tears in his eyes as his team, playing another new formation, finally managed to win their first game after five defeats and two draws in Serie A.
"I did have tears, because that's how much I care about my team," said Ranieri, who took over in September and revitalised ageing Inter before the sudden slump.
"This result is great for our morale. Now let's hope our fans at San Siro get behind us and lift us up, in order to help us qualify to the next round of the Champions League (against Olympique Marseille).
"It won't be easy, but we'll take all of our chances."