The Rangers didn’t let their losing streak reach four games, hanging on in the third period to edge the Oilers, 4-2, Tuesday night at Madison Square Garden.

Leading 3-2 after two periods on goals by Mats Zuccarello, Dylan McIlrath and Rick Nash, the Rangers survived three big penalties early in the third, two on Marc Staal, for slashing and high-sticking.

But the Rangers held the Oilers, who played Monday in Boston, without a shot on two of the three opportunities, and just two on the other. The third penalty, and sixth of the evening, went to J.T. Miller for a high-stick at 8:46. Jesper Fast’s empty netter with 3.2 seconds remaining salted it away.

Zuccarello and Nash had a goal and an assist, Keith Yandle had two assists, and McIlrath’s goal was his first in his 16th NHL game as the Blueshirts improved to 19-9-4 overall, 13-4-1 at home. The Oilers, who had won six straight games, dropped to 5-11-1 on the road.

The Rangers were up 1-0 after the first period, but four goals were scored in the second.

With Oscar Lindberg in the box for roughing, his second penalty of a tough night for the Swedish rookie, Taylor Hall closed in on the left and beat Henrik Lundqvist far side for the 1-1 tie at 5:12.

McIlrath restored the lead with a one-timer blast from the left point past Anders Nilsson at 7:54. It was set up by Yandle, his second assist of the night. But another rookie defenseman, Brady Skjei, 21, who had been solid for the Rangers in his NHL debut, coughed up the puck in mid-ice, triggering a breakaway by Jordan Eberle. Lundqvist went for the deke, lunged and Eberle easily lifted the puck over him at 18:20.

At 19:08, the Oilers were whistled for too many men on the ice. Nash, at the right post, found the net 13 seconds later to send the Blueshirts to the locker room ahead 3-2. His 10th goal of the year proved to be the game-winner.

With more jump and determination early than they showed on the winless three-game trip, the Rangers opened the scoring at 5:04 of the first after killing Lindberg’s hooking penalty. Zuccarello took a pass from Yandle, strode to the left dot and sniped the puck into the top left corner past Anders Nilsson for his team-leading 14th. It was his third straight game with a goal.

Derick Brassard won a board battle with Taylor Hall and was tripped at 10:30. But the Rangers didn’t cash in on the power play, with shots missing high and wide. The Rangers seemed to lose some steam, and with 5:40 left, McIlrath’s high-sticking penalty forced them to fend off another man-advantage, allowing just one shot on Lundqvist.

Skjei, called up from Hartford, stepped in for Dan Girardi (swollen right knee) and played a solid game. Girardi hadn’t missed a game with an injury since Feb. 7, 2013.