The Dodgers had trailed 1-0 since the fourth inning but got the tying run in the seventh when Gonzalez knocked in Jerry Hairston Jr. with a one-out single.
In the eighth, Skip Schuamker collected a leadoff double against Nick Vincent (3-2) and went to third on a Todd Fedorwicz sacrifice.
Ellis then came up to hit for Brian Wilson (1-0) and singled to center for the decisive blow.
Kenley Jansen struck out the side in the ninth for his 23rd save as the National League West-leading Dodgers clinched their three-game series.
Ronnie Cedeno homered for San Diego's only run.