In his first four games with his new team, rookie Didi Gregorius displayed a line-drive swing, a smooth glove and a strong arm. But in his fifth game on Tuesday night, it was his aggressiveness on the bases that helped the Diamondbacks avoid another gut-wrenching loss at AT&T Park.

Gregorius hustled into second base to surprise the San Francisco Giants with a one-out double in the 11th inning on Tuesday night. On the next play, he made a cavalier dash to third base, perhaps forcing an error. And moments later, he raced home on a wild pitch with the go-ahead run, helping the Diamondbacks to a 6-4 win in a game in which they blew a 4-0 lead.

As usual, the Diamondbacks did not win easily. Nor did the Giants go down without a fight.

Diamondbacks starting pitcher Pat Corbin gave up just three hits through seven scoreless innings, taking a 4-0 lead into the eighth. But the Giants began the bottom of the eighth with back-to-back hits by Andres Torres (single) and Brandon Crawford (triple), and an RBI groundout by Hector Sanchez cut the lead to 4-2.

Then, for the second night in a row, the Giants made an improbable comeback, this time getting a two-run homer from pinch-hitter Brandon Belt, who rocketed a hanging off-speed pitch from closer J.J. Putz into McCovey Cove beyond the right-field wall.

And so in two days, three Diamondbacks relievers — Putz, right-hander David Hernandez and left-hander Tony Sipp — have given up key runs in tight games.

But thanks to Gregorius — and to an uncharacteristically sloppy 11th inning by the Giants — the Diamondbacks weren’t finished.

With one out in the top of the 11th, Gregorius dropped a single into left-center field, where Giants left fielder Torres fielded it on a hop.

But as Torres appeared to momentarily forget that one of his duties as an outfielder was to return the ball to the infield, Gregorius tore around first base and slid headfirst into second. One batter later, on a ground ball to third base, Gregorius waited for Pablo Sandoval to throw to first before taking off. First baseman Brandon Belt could not come up with the one-hop throw, perhaps because he already was thinking about firing back across the diamond to nail Gregorius.

And so with runners on the corners, the Diamondbacks were in position to take the lead — before the Giants went ahead and gave it to them. On a 1-0 pitch to Gerardo Parra, Giants reliever Santiago Casilla fired a fastball in the dirt that catcher Buster Posey couldn’t handle, and Gregorius ran home with the go-ahead run.

The late-game dramatics overshadowed just how good Corbin was early in the game. Throwing strike after strike, he was locating his low-90s fastball with precision before putting hitters away with his sharp slider, a pitch he says has improved since last season.

For Corbin, who won the fifth starter’s job easily in spring training, it was his fourth consecutive strong outing to start the season. After the 71/3-inning, two-run effort, Corbin has a 1.71 ERA and the Diamondbacks are unbeaten in his starts.