"Certainly there's a little irony involved in the whole thing," A's manager Bob Melvin said.
Donaldson's drive over the wall in center field on a sinker from Zach Britton (3-2) sent the A's streaming out of the dugout in front of an announced crowd of 27,232 at the Coliseum.
It was the All-Star third baseman's 21th homer of the season, fifth career game-ending hit, and third career game-ending homer -- including the second this season.
But this one was even sweeter for Donaldson and the A's because it took the attention away from Machado, who hit a go-ahead two-run homer in the seventh after creating an even bigger stir with his bat the last time he faced Oakland.
Machado threw his bat near Donaldson at third base during a plate appearance in Oakland's win at Baltimore last month after reliever Fernando Abad threw consecutive high-and-inside pitches.
Machado was suspended five games and was fined an undisclosed amount. Abad was fined but not suspended.
In the opener of that series, Machado yelled in Donaldson's face after the third baseman tagged him on the chest, knocking Machado off his feet. The dugouts cleared briefly.
Machado, who also singled in the fifth, said before Friday's game he has moved on from the incident. Melvin also said nobody on his club had even talked about it since.
"When you see J.D. come up in the ninth, you kind of have that feeling, `Really? Could this happen?" Melvin said. "And it did."
Donaldson said he sympathized with the boos Machado endured from A's fans during pregame introductions, when he ran to his spot at third base and every time he was announced at the plate. But he said he tried "not to get caught up in all that jazz" and instead focus on the task at hand.
"I enjoy those moments," he said. "It's one of those things where you've got to try to relish the opportunity, and anytime I can step up to the plate with an at-bat to help our team win the game or win the game right there, I feel better about it."
Machado quieted the hoots and hollers with his 10th homer of the season in the seventh to put the Orioles ahead 4-2. A fan threw the ball back on the field, and others stood and booed as he rounded the bases.
Machado high-fived teammates back in the dugout.
"The booing is part of the game and I have to get used to it," Machado said. "You have to be able to block it out. You just try and channel your energy to do well. I have to gain everyone's respect back from everyone that I let down."
Jonathan Schoop hit a two-run homer in the fifth that appeared to be enough for the AL East-leading Orioles. But beating the best team in baseball -- especially at home -- has never been easy this season.
Eric O'Flaherty (1-0) tossed one scoreless inning for the win as Oakland (60-36) extended the majors' best record.
It was just the type of victory that showed why the two-time defending AL West champion A's have major expectations, especially after acquiring pitchers Jeff Samardzija and Jason Hammel from the Chicago Cubs on July 4.
Samardzija looked ready to lose his second straight solid start for Oakland. He gave up six hits -- including the two homers -- in seven innings.
Baltimore had retired 10 straight batters until Yoenis Cespedes singled leading off the ninth. Brandon Moss followed with another single before Donaldson's homer handed Britton his third blown save in 18 opportunities this season and gave the A's another memorable victory in a season already full of them.
Cespedes, the two-time reigning Home Run Derby champion, snapped an 0-for-19 skid -- the longest hitless streak of his career -- with two singles. ... Orioles RHP Ubaldo Jimenez, on the disabled list since July 11 with a sprained right ankle, is scheduled to throw a bullpen session Sunday and could make a rehabilitation start in the minors in the middle of next week. ... RHP Jason Hammel (0-1, 3.60 ERA) starts for the A's opposite Orioles lefty Wei-Yin Chen (9-3, 4.15 ERA) on Saturday.