HOUSTON — Astros manager A.J. Hinch joked that his team’s improbable 4-3 win over the Oakland Athletics on Wednesday was premeditated.

“We had it all mapped out that way,” Hinch said. “We were going to get multiple triples and a wild pitch strikeout. Exactly how we drew it up for the sweep.”

Evan Gattis hit an RBI single to cap a three-run rally in the eighth inning keyed by a wild pitch on a strikeout.

Reliever Liam Hendriks (0-3) got two quick outs in the eighth before George Springer tripled. Alex Bregman then struck out, but the ball got away on a wild pitch.

Catcher Bruce Maxwell chased it to the backstop and made a desperation throw to first, but Bregman was safe by a step and Springer scored.

Jose Altuve tripled home the tying run and Gattis singled for the lead.

“Every win is big, but this one was big for us,” Altuve said. “It keeps our momentum going. We’re really playing really good ball. We’ve got to keep going.”

The Astros began the day two games behind Baltimore for the second AL wild-card slot. Houston swept the three-game series and next opens a three-game set at AL West-leading Texas.

A’s starter Ross Detwiler gave up three hits and left after the seventh with a 3-1 lead.

Michael Feliz (8-1) pitched a scoreless eighth. Ken Giles closed for his seventh save.

“My teammates pulled through and never gave up throughout the game,” Giles said. “It’s fun to watch them battle every inning and learning from every at-bat. It’s one special thing to watch.”

The Athletics have lost 12 of their last 18 games.

Detwiler had given up 14 runs and 23 hits in his previous three starts. He shut down Houston, striking out seven. Carlos Correa hit an RBI double for the only run off Detwiler.

“He had good stuff today, man,” Gattis said. “He pitched in today well and had a good changeup. We just couldn’t get anything going with him.”

The A’s got all three of their runs in the second off Mike Fiers. Jake Smolinski hit a two-run triple and Bruce Maxwell had a sacrifice fly.

Fiers gave up eight hits and three runs — one earned — in five innings. After he was gone, the Astros rallied.

“That’s how this team is. We don’t stop, we battle,” Fiers said. “No one takes their at-bats off, no one takes pitches off, and we just find ways to win. It’s always fun watching this team, and it should be good going forward.”

TRAINER’S ROOM

Athletics: LHP Sean Manaea (sore back) will miss his next scheduled start on Sunday. He left his start last Monday vs. Houston in the fourth inning. Kendall Graveman, who allowed three runs in seven innings in a loss to the Astros on Tuesday night, will start in place of Manaea.

Astros: INF Marwin Gonzalez left with discomfort in his right hamstring. In the bottom of the fifth, he hit a flyball onto Tal’s Hill in deep center field and started limping after rounding first. He was replaced by A.J. Reed in the top of the sixth. Gonzalez missed several games four weeks ago with right hamstring soreness.

UP NEXT

Athletics: RHP Andrew Triggs (1-1, 4.39 ERA) takes the mound against Boston on Friday. Triggs picked up his first career win in his last start, allowing four hits and three runs while striking out eight in six innings pitched against St. Louis.

Astros: RHP Doug Fister (12-9, 3.60 ERA) starts against Texas on Friday, replacing Dallas Keuchel, Hinch announced after Wednesday’s game. Fister has alternated wins and losses in his last four starts since allowing nine hits and one run in six innings in a 3-2 loss to Texas on Aug. 6.

KEUCHEL TO GET EXTENDED REST

Astros LHP Dallas Keuchel was originally slated to start on Friday when the Astros open up a three-game series at Texas, but RHP Doug Fister will start in his place, Hinch said. The reigning AL Cy Young Award winner had a rebound month in August, going 3-1 with a 3.10 ERA, but cited lingering soreness as a reason for pushing his start back. The Astros starters for Saturday and Sunday are to be announced.

“It’s nothing major. Just a little more rest than usual,” Keuchel said. “I’ve been a little more sore than usual, and with the off day, we can keep the rotation the same so it really works out better than I had hoped.”

DOG DAYS?

The Astros finished the month of August with a 16-13 record, the club’s best August since 2010 when the team went 17-12. The Astros closed out the month by going 10-2, including a 5-1 homestand.