Pellegrini blasted home in stoppage-time, minutes after Daniele Verde had pulled Spezia level, with relieved Roma coach Paulo Fonseca racing to hug the midfielder.
"The hug was a team moment that shows that we are all together," Fonseca said. "We showed great spirit. It was a game which was essential for us to win."
The capital side has been in turmoil since its chaotic 4-2 extra-time loss to Spezia last week.
The official scoreline was changed to 3-0 on Saturday (AEDT), as Roma made six substitutions, with just five allowed, during the Round of 16 clash which it finished with nine men.
Captain Edin Dzeko was left out of the team on Sunday (AEDT), officially because of a muscular injury, but amid reports of a fall-out with Fonseca.
Borja Mayoral scored a double for the hosts, netting the first after quarter of an hour, only for Roberto Piccoli to pull 14th-placed Spezia level seven minutes later by pouncing on a mistake by home goalkeeper Pau Lopez.
Real Madrid loanee Mayoral slotted home his second on 52 minutes, before Rick Karsdorp tapped in to make it 3-1 three minutes later.
Diego Farias again responded for the visitors just before the hour, scoring the third goal of a dramatic six-minute spell at Stadio Olimpico.
An error from Chris Smalling allowed former Roma winger Verde to equalise in the 90th minute, firing home left-footed after the England defender missed his attempted clearance.
Pellegrini had the final say, though, drilling the ball into the roof of the net from close range after Bruno Peres's unselfish cutback.
It was a first league win in three games for Roma, after last week's defeat by Lazio in the Rome derby and a draw with title rival Inter Milan.
Roma moves ahead of Napoli into third on the Serie A table.