Max Scherzer also pitched seven sharp innings as the D-backs won their season-high fifth game in a row.
Lopez's shot came after pinch-hitter Alex Romero gave the Diamondbacks a 3-2 lead with an RBI single.
Arizona had resided in the cellar since June 8, but has vaulted over the Padres into fourth place in the NL West. Arizona's winning streak is its longest since an eight-game streak in April 2008.
Will Venable and Kevin Kouzmanoff each had RBI doubles for San Diego, which has dropped seven of its last eight.
The Padres will open a four-game set against the Giants tonight in San Francisco.