Kozlov's wrist shot from the slot beat Kevin Weekes to the glove side with 6:08 left. He completed the scoring on a wrist shot with 1:36 left.
With their fifth victory in seven games, the Islanders pushed their point total to 31, one more than the Rangers and New Jersey Devils.
Miroslav Satan and Chris Simon each had a goal and assist for the Islanders, while Arron Asham also scored. Rick DiPietro made 39 saves.
Jaromir Jagr had the other goal for the Rangers in their first regulation loss in seven games (3-1-3). Henrik Lundqvist started and stopped four of eight first-period shots. Weekes made 10 saves in relief in the final two periods.
Just 12 seconds after killing off a penalty, the Islanders opened the scoring when Mike Sillinger fed Satan on the right wing. Satan moved to the lower right circle and wristed a shot high to Lundqvist's short side.
Satan, who led the Islanders with 35 goals last season, was off to a slow start, but has five of his seven goals in the last nine games.
The Islanders cashed in on their first power-play chance for a 2-0 lead. Point men Radek Martinek and Satan were playing catch when Martinek found Simon down low. His turnaround wrist shot banked in off the left post.
Kozlov put the Islanders up by three with 3:32 left on a backhander from the lower left circle. Asham stepped out of the penalty box and scored on a wrist shot from the right circle with 33.4 seconds left in the period.
Weekes replaced Lundqvist to start the second period, and passed his first test, stopping Shawn Bates with a pad save on a short-handed breakaway at 4:38.
Shanahan put the Rangers on the board with a short-handed goal at 6:45. On a 2-on-1 rush, Shanahan used teammate Matt Cullen as a decoy and fired a wrist shot between DiPietro's pads.
Jagr picked up his 13th goal at 16:22 on a wrist shot and, 66 seconds later, Shanahan knocked down a puck and quickly wristed a shot past DiPietro. Jagr has the most career goals against the Islanders in franchise history with 52.
Kozlov then scored his second goal of the night on a rebound at 18:41.
The Rangers' Michael Nylander had a goal disallowed with 64 seconds left in the period because the referee's whistle blew before he was able to jam a loose puck home.
Shanahan picked up the hat trick at 7:01 of the final period on a power play. He found a loose puck at the left crease and slammed it home.
Martin Straka earned an assist on the goal, extending his points streak to seven games (6-7-13).
Notes: It was the first hat trick of the season for both Shanahan and Kozlov, and Kozlov's second career hat trick. ... Simon was clipped with an accidental high stick with 8:58 to go. He was helped off the ice with a towel covering his face. Ranger fans found a new target to direct their booing toward: former Blueshirt defenceman Tom Poti, who has played well for the Islanders. ... The scheduling for the eight games between these two rivals features the oddity of the Rangers hosting four of the first five meetings. If last season was any indication, it wouldn't matter to the Rangers, who won three of four games at Nassau Coliseum.