The victory made his one-car team for JTG Daugherty Racing eligible for the Chase for the Sprint Cup title. Allmendinger held off Ambrose through the first two turns and opened a lead after both cars bumped and won going away.
Three-time Cup champion Tony Stewart pulled out of the race 12 hours after he struck and killed a sprint car driver who had climbed from his car during a race in nearby Canandaigua, New York.
Allmendinger offered his condolences to the Ward family after he won, saying "we're a community here, we're thinking about you."
In a statement released during the race by a spokesman, Stewart said: "There aren't words to describe the sadness I feel about the accident that took the life of Kevin Ward Jr. It's a very emotional time for all involved, and it is the reason I've decided not to participate in today's race at Watkins Glen. My thoughts and prayers are with his family, friends and everyone affected by this tragedy."
Regan Smith drove Stewart's car instead and finished 35th after having to start from the back of the 43-car field and getting caught in a late accident.
It was the second straight time Stewart missed the race at The Glen, where he has a NASCAR-record five victories. He suffered a broken leg in a sprint car accident in Iowa days before the Cup race and missed the rest of the season.
There were three restarts in the decisive closing laps.
Allmendinger got the jump on Ambrose in the first one with nine laps to go, but Jimmie Johnson was bumped in the first turn and his No. 48 spun around, collecting two other cars and precipitating a caution.
The race restarted again with five laps left and both Ambrose and Allmendinger were side-by-side through the esses, banging against each other before Ambrose took the lead in the chicane as he's done so many times in the past, dirt flying as both cars hit the grass.
Allmendinger wasn't done, regaining the lead on the next lap as he outbraked the Australian into turn 1 before a crash involving Denny Hamlin caused another red flag with four laps left.
The 90-lap race on the 2.45-mile layout was red-flagged for 1 hour, 21 minutes just past the midpoint after a violent crash involving Ryan Newman and Michael McDowell that involved three other cars.
Newman's Chevrolet spun hard into the Armco barrier lining the track, ripping a big hole in the barrier's metal. The car then spun around twice and went back across the racing surface, collecting McDowell in his No. 95, which incurred heavy damage in the rear. Newman, McDowell and Alex Bowman were treated in the infield care center and released.
Allmendinger gained the lead on lap 64 after pit stops, with Kurt Busch and Ambrose close behind.
Ambrose outbraked Busch into Turn 1 for second two laps later and set his sights on Allmendinger, who had a 2-second lead that was wiped out by the late stoppages.
Jeff Gordon started from the pole and led the first 29 laps, holding Ambrose at bay. But Gordon's engine died on lap 50 as he lost all power and couldn't get it refired, spoiling a promising day. Gordon was second to Ambrose after the first round of pit stops and slightly faster when his car stalled.
Defending race winner Kyle Busch had to do a pass-through penalty after a fuel can stuck in his No. 18 Toyota. Back on the track, he shredded his left front tire after contact with Martin Truex Jr. and had to pit again and was out of contention.
Brad Keselowski, who was second the previous three races at The Glen, had contact early and brake problems and finished five laps back.