The Padres built a five-run lead, with Headley powering a three-run homer in the third, but Arizona came back to tie it in the top of the eighth.
Headley wasted no time putting San Diego back in front, hitting a leadoff homer in the bottom of the inning off David Hernandez (2-3).
That made a winner out of Brad Brach (2-4), who was the third San Diego pitcher to work in the top of the eighth and who retired the only batter he faced to end the inning.
Luke Gregerson, the seventh San Diego pitcher of the night, survived a two-out single in the bottom of the ninth for his fifth save.
After San Diego took a 5-0 lead in the third on Headley's first homer, Arizona fought back on a two-run double by Ryan Wheeler in the fifth, an RBI double by Wheeler in the seventh and Miguel Montero's two-run double in the eighth.
Both teams had won their previous two games.