The Giants won for the first time in four tries, coming through with the game-winning hit off Luke Gregorson (1-2).
Gergor Blanco opened the ninth with a single, but he was erased on a force play when Torres was unable to execute a sacrifice.
Torres, after reaching first on the failed bunt attempt, stole second and scored when Pagan doubled to right.
Santiago Casilla (2-1), the fourth San Francisco pitcher, picked up the win by working the final 1 1/3 innings without giving up a hit.
San Diego tied it at 2-2 on a solo homer by Chase Headley in the sixth. The Padres had won their previous three games.