He tipped a Ryan Strome point shot past Lightning goalie Andrei Vasilevskiy.
Kevin Fiala had two goals, and Calle Jarnkrok and Ryan Johansen also scored for Nashville, which improved to 9-1-2 in its last 12 games.
On the winner, Nick Bonino sent a saucer pass across the front to Josi, who tapped it into the open net with Islanders goalie Jaroslav Halak out of position.
John Tavares, Ryan Pulock, Casey Cizikas and Nick Leddy scored for New York, which lost for the third time in four games after the All-Star break.
On the power play, Hanzal skated across the goalmouth and tipped in Julius Honka's shot from the right point to break a 1-all tie 4:05 into the third period.
The Rangers, who have lost six of seven, are last in the rugged Metropolitan Division and three points out of a postseason spot in a crowded Eastern Conference playoff race.
Mitch Marner, Leo Komarov and Zach Hyman scored one apiece, but the win might have come at a cost.
- McDavid scores 4 goals, rising Oilers beat Lightning 6-2WPXI Pittsburgh
- Player grades: White-hot Connor McDavid strikes four times as Edmonton Oilers ...Edmonton Journal
- Oilers' McDavid bolsters case for title of NHL's best playerSportsnet.ca
- Connor McDavid spectacular as Edmonton Oilers light up LightningGlobalnews.ca
- Connor McDavid runs wild on Tampa Bay, leads Edmonton Oilers to upset victoryEdmonton Sun
- Lightning journal: Dan Girardi progressing toward returnTampabay.com
- Lightning road trip ends with a thud in EdmontonTampabay.com (blog)
- Watch: Connor McDavid Scores From Impossibly Tight AngleSports Illustrated
- NHL roundup: Oilers' McDavid scores four goalsYahoo Sports