"That's amazing," said Oakland manager Bob Melvin. "For a late-inning at-bat, that's as good as I've ever seen in a team. The 27th out, we don't give that away. It's an attribute. That's the way this team is."
The Astros brought in Chad Qualls in the ninth inning to close out the game. But Qualls couldn't finish the inning, allowing five runs while recording just two outs.
Down 4-1, the A's comeback started when pinch-hitter Alberto Callaspo singled in Derek Norris and Josh Reddick, whose home run leading off the fifth inning had tied the game at 1.
The A's were eventually down to their final out. But Yoenis Cespedes' third hit of the game tied it at 4. The A's went ahead 5-4 when Brandon Moss singled off Tony Sipp. Josh Donaldson added to the barrage with a two-run double off Jose Veras.
"Once we get a few guys on, the confidence builds," said Moss, who had four weak outs before his go-ahead hit. "A lot of good things happen when you get guys on base. A few things went our way in the ninth. We know we have a good lineup."
It's a lineup that Samardzija appreciates. Samardzija allowed four runs in 6 2/3 innings, giving up six hits, including a Marwin Gonzalez home run, the second batter he faced.
"I'm not happy how I pitched," said Samardzija, who failed to go seven innings for the first time with the A's. "It was 1-1. I had a chance to keep it there and I didn't.
"Every pitch and every run is important. But I did keep our offense to where they were in striking distance.
The beneficiary of the six-run ninth inning was Evan Scribner (1-0), who pitched a scoreless eighth inning. Sean Doolittle pitched a scoreless ninth for his 16th save.
This was the sixth win of the season for Oakland when trailing after eight innings. And it's the second time this season they have scored at least six runs in the ninth inning. They scored seven runs against the Astros in Houston on April 25 in a 12-5 win.
"We've been doing this for a while," said Melvin, describing his team that leads the majors with 534 runs.
Scott Feldman was in line for the win, leaving with a 4-1 lead after seven innings of five-hit ball. Qualls (1-2) failed to protect the lead and got the loss.
"They make contact," said Qualls. "They take a lot of pitches. Two of the hits they got tonight weren't hit really hard. Norris' to start everything off was just a little nubber to third, and then Cespedes that tied it was just a jam shot they got to the outfield.
"They found some holes when they needed to and hit the ball on the screws when they needed to. I just didn't get it done."
Reddick had two of the A's 11 hits and scored twice. His home run was his sixth of the season.
"This is a great ballpark," said Reddick. "I get jammed on one and it goes out of the ballpark."
The Astros went ahead after the A's lost a fifth-inning replay challenge that led to Houston scoring twice, taking a 3-1 lead.
Robbie Grossman was ruled safe on a stolen-base attempt with one out. After the safe call stood, Enrique Hernandez tripled off Samardzija, scoring Grossman. Jose Altuve followed with a single, giving the Astros a two-run lead.
Samardzija allowed Gonzalez's fifth home run of the season, just inside the right-field foul pole, giving the Astros a 1-0 lead. Samardzija fared better the next time against Gonzalez. In an 11-pitch at-bat, Samardzija struck out Gonzalez swinging, leading off the Houston fourth.
Samardzija was chased from the game with two outs in the seventh after Hernandez's single scored Jon Singleton, giving the Astros a 4-1 lead.