Shanahan, who came to the Rangers after netting 40 goals last season with Detroit, has scored in all but one of New York's six games - including four power-play tallies. Aaron Ward and Jason Ward also scored in the second period for the Rangers, who snapped a three-game losing streak in which they allowed 16 goals.
Martin Straka's third goal with 3:18 left sealed it. New Jersey rookie Travis Zajac had brought the Devils within 3-2 at 1:37 of the third period.
Scott Gomez also scored for New Jersey, which lost despite a 7-1 advantage in power plays. Shanahan made the most of the Rangers' only chance, and Jason Ward scored while New York was short-handed.
Henrik Lundqvist made 34 saves and threw his stick into the crowd after being chosen as the game's first star.
Aaron Ward broke the scoreless deadlock with his first goal since leaving the Stanley Cup champion Carolina Hurricanes this summer. And he seemed as surprised as anyone to see the red light go on behind Martin Brodeur.
The Devils goalie whacked the puck up the right-wing boards, causing it to bounce along the choppy ice to Ward, stationed at the point. The defenceman fired a slap shot that whizzed by teammate Blair Betts and forced Rangers forward Ryan Hollweg to duck in front. It avoided everyone and nestled into the top left corner of the net to make it 1-0 at 8:24.
The lead lasted 35 seconds before Gomez tied it. Brian Gionta, who scored four goals in two games, nearly had another but had to settle for an assist as Gomez swept in a rebound of his shot that slipped behind Lundqvist at 8:59.
It took until only 3:09 was left in the second period before the Rangers received a power play. The crowd acknowledged it with a sarcastic standing ovation. Shanahan whistled a shot from above the left circle past Brodeur's glove at 18:29.
Jason Ward then followed up Matt Cullen's rebound to make it 3-1 with under 35 seconds remaining in the period.
Although they were sloppy, the Rangers managed to withstand the Devils' pressure - helped greatly by Lundqvist's play in the second period.
New York bounced back after showing few signs during the losing streak of the disciplined style coach Tom Renney implemented last season during the team's unexpected run to the playoffs that ended with a first-round sweep by New Jersey.
Notes: Brodeur, third on the NHL career win list, is one victory shy of 450. He trails only Patrick Roy (551) and Florida's Ed Belfour (457). ... Rangers D Thomas Pock was scratched for the first time this season to make room for Darius Kasparaitis' season debut. ... New Jersey recalled 30-year-old defenceman Alex Brooks from Lowell of the AHL to fill in for Brad Lukowich (ankle).