A game-ending strikeout of pinch hitter Chris Iannetta turned out to be the perfect remedy.
Doolittle gave up two hits and a walk in the ninth inning but recorded the final two outs for his 19th save Friday night, preserving Oakland's 5-3 win over the Los Angeles Angels in a matchup of American League West rivals.
"They're not all going to be pretty, but that was a really, really important win to be able to nail down," Doolittle said. "That was a huge game for us. There was a lot of adrenaline flowing."
Coco Crisp hit his 15th career leadoff home run and Stephen Vogt added a solo shot for Oakland in the opener of a three-game series. Sam Fuld hit a go-ahead triple in the sixth inning, and the A's pulled within one game of the division-leading Angels.
Angels manager Mike Scioscia noted before the game that "the pennant race is in full bloom."
The night had that type of feel, too.
Gray (13-7) allowed three runs and six hits over 8 1/3 innings to end a monthlong losing streak. He settled down after a shaky start to win for the first time since July 26.
Oakland's Opening Day starter, Gray had lost his previous four starts -- that skid coming after he had won six straight decisions.
Gray struck out five, walked two and left to a rousing ovation from the crowd of 33,810.
Fuld tripled off reliever Jason Grilli (1-2) as part of Oakland's two-run sixth.
"We haven't played a game like that, that crisp, for a while," A's manager Bob Melvin said. "(When) we took the lead, it really felt like we were finally in charge of a game, where we hadn't been in a while."
"There were a lot of good things on the field and some things we didn't get done," said Scioscia, whose club is 0-4 at the Oakland Coliseum this season. "We haven't gotten it down up here yet this season."
The Angels, owners of the best record in the majors, didn't get much rest before the game. They completed a four-game sweep of the Boston Red Sox late Thursday, then took a red-eye flight to California. Players and coaches didn't make it to their hotel in San Francisco until 4 a.m. Friday.
Trout hit his 28th home run off Gray in the first while Hamilton belted his solo shot with one out in the fourth.
The A's scored on Crisp's leadoff home run in the first. He also doubled and scored on a throwing error by Angels shortstop Erick Aybar in the fifth.
Angels starter Hector Santiago allowed two runs on five hits in five innings. The left-hander was replaced two batters after getting hit on his pitching hand by Josh Donaldson's line drive. Santiago's hand was swollen, but he said he expected to make his next start, according to MLB.com.
Crisp's leadoff home run was his 13th with the A's, tying him with Bert Campaneris for the third-most in franchise history. It also marked Crisp's second multihit game in the past four days. Prior to that, he hadn't had one since July 8.
Angels: RHP Garrett Richards underwent surgery to repair the torn patellar tendon in his left knee. Scioscia said Richards' surgery "went as expected." He added that the timetable for Richards' recovery remains six to nine months.
Athletics: OF Craig Gentry (broken right hand) was reinstated from the 15-day disabled list. RHP Dan Otero was optioned to Triple-A Sacramento. ... First baseman Kyle Blanks is having pain in both feet and his lower legs so the team scheduled him for an MRI.
Angels: LHP C.J. Wilson (10-8) is 4-0 with a 2.60 ERA in five starts against the A's last season.
Athletics: LHP Jon Lester (13-8) looks to bounce back from his first loss in an Oakland uniform and his first defeat since June 7 while with Boston.
The Associated Press contributed to this report.