Gamarra converted from the spot to give New York a 3-2 lead in the 84th minute after the Galaxy's Dave Romney was called for a hand ball in the area.
Luis Robles had three acrobatic saves, including two diving stops and a leaping deflection, for the Red Bulls.
Daniel Royer put away a cross from Wright-Phillips to open the scoring in the seventh minute. Gamarra, near midfield, lofted a pass to Wright-Phillips on the right side of the box, where he fed a streaking Royer for the tap-in finish into a wide-open net.
Florian Valot doubled the advantage in the 49th minute, lifting a cross from Wright-Phillips in the top of the net on a breakaway. Gamarra chipped it to Wright-Phillips on the left flank, who tapped it to Valot at the top of the six-yard box.
L.A.'s Ola Kamara put away a through ball from Zlatan Ibrahimovic in the 59th minute and Giovani dos Santos scored in the 66th to make it 2-2.
The Galaxy have lost three in a row at home.
LA Galaxy 2-3 NY Red Bulls: Red Bulls outlast Galaxy - Via MLS
The New York Red Bulls benefitted from a couple VAR calls and fended off a late LA Galaxy rally to win 3-2 at StubHub Center.