SAN DIEGO — The Arizona Diamondbacks came into a three-game series against the Padres with the worst record and worst ERA in the majors.

The D-backs won the first two games and took a 3-1 lead in the fourth inning on Sunday before letting San Diego tie the game, then win it 4-3.

Even so, Arizona (11-23, .324) leaves town having cracked double digits in wins and passed Houston (10-21, .323) by one percentage point for the big-league low.

“I feel good about where we are at,” said D-backs catcher Tuffy Gosewisch, who hit a two-run single in the fourth inning. “We did some good things in this series, and I think things are headed in the right direction.”

Cameron Maybin reached after an overturned umpire’s call, then scored on Yonder Alonso’s slow grounder in the ninth as San Diego avoided a three-game sweep.

Maybin easily beat the throw home by first baseman Paul Goldschmidt, who was playing in with one out.

The Padres snapped a four-game losing streak and scored their most runs in five games.

Maybin started the winning rally with a chopper to second off Oliver Perez (0-1). He initially was called out, but Padres manager Bud Black challenged. The call was changed, giving Maybin an infield single. It made Black 2 for 2 in challenges on Sunday and 4 for 4 for the season. Rene Rivera, who earlier hit a two-run double, doubled over right fielder Gerardo Parra’s head to move Maybin to third.

Huston Street (1-0) pitched the ninth for the win.

Arizona starter Wade Miley gave up three runs and six hits in seven innings — snapping a streak of 20 scoreless innings by D-backs pitchers — striking out four and walking one.

Arizona went ahead 3-1 in the fourth after Goldschmidt hit a leadoff homer off Tyson Ross and Gosewisch singled in Cody Ross and rookie Ender Inciarte.

The D-backs continue their trip today by opening a three-game set at Milwaukee. Arizona’s Mike Bolsinger (1-1, 5.79) is set to start against Matt Garza (1-3, 5.00).