Gillaspie's homer snapped a 1-1 tie in the sixth, and Dunn's 22nd, a two-run homer in the seventh, gave Chicago a 5-1 lead. Addison Reed pitched a scoreless ninth for his 22nd save in 26 attempts.
More From ESPNChicago.com
Can't get enough White Sox information? ESPNChicago.com has all the latest from the South Side. Blog
Chicago ended a five-game losing streak and avoided falling a season-high 16 games below .500.
And the White Sox managed to win by playing the kind of clean game they haven't for most of the season.
The White Sox made no errors, their bullpen was solid, the pitching was strong, and the offense produced. It all added up to a needed victory against one of baseball's hottest teams.
"It's nice," White Sox manager Robin Ventura said. "We've had some tough nights here the last few."
The White Sox have lost 23 of their past 32 and are in last place in the American League Central.
Danks (2-5), who earned his first win since beating Oakland on June 8, allowing two runs and six hits and seven innings with four strikeouts, knows what kind of hole the White Sox have dug for themselves.
"We know how good we are. We know we've got a hell of a road ahead of us. We did it to ourselves," Danks said. "But at the same time, we know how capable we are of going on a run. We just hope that we're able to do it starting now. We know we're running out of time."
Baltimore took a 1-0 lead with one out in the third when Brian Roberts hit his first home run of the season after coming off the disabled list Sunday. Roberts' homer, on Danks' first pitch of the at-bat, was his first since April 13, 2011, against the Yankees.
That was all the production Baltimore got off the White Sox until a run in the eighth.
"(Danks) didn't present many opportunities. He had a good tempo and we knew what he was going to try to do and he did it," Orioles manager Buck Showalter said. "That's a tribute to him and the good stuff he took out there tonight. He had a good change-up."
Chicago's offense gave Danks more than he needed.
Chicago tied the game in the bottom of the fourth on Beckham's one-out single to left. The White Sox took a 2-1 lead on Gillaspie's one-out homer in the sixth. On a 1-0 pitch, Gillaspie hit his sixth homer of the season, a line drive that landed in the right-field corner.
Chicago scored three times in the seventh, capped by Dunn's 427-foot, two-run homer. The homer was Dunn's 428th of his career, passing Mike Piazza for 45th all time, and gave Chicago a 5-1 lead on the second of two homers off Jason Hammel (7-5), the 17th and 18th he's allowed this year.
"I don't really know how to answer that. It's just balls that are up in the zone that get hit out this year that weren't last year," said Hammel, who pitched seven innings and gave up five runs and nine hits.
"It's kind of just the way I'm going right now. The mistakes are being amplified."
Tuesday's game capped a busy day for the Orioles, who acquired starting pitcher Scott Feldman and catcher Steve Clevenger from the Cubs for starter Jake Arrieta, reliever Pedro Strop, and two international signing bonus slots. Feldman, who signed a one-year deal with the Cubs last offseason and is 7-6 with a 3.46 ERA, is expected to start Wednesday for the Orioles.
Beckham hopes Tuesday is the start of a run for the White Sox, one they need to get back into the race.
"Show up tomorrow and try to win tomorrow and see what happens," Beckham said. "Ten game winning streaks have happened before, so we'll continue to show up and do the work and hopefully get on a roll here."
Paul Konerko was the White Sox's designated hitter and went 0 for 4 after missing six games with lower back tightness. ... The White Sox signed 16-year-old Dominican outfielder Micker Adolfo Zapata to a contract that included a $1.6 million signing bonus, the largest in team history for an international signing. ... The White Sox appointed former slugger Jim Thome to a position as special assistant to general manager Rick Hahn. Thome, who last played for Baltimore in 2012, will work with players at all levels throughout the organization. ... With Feldman pitching Wednesday against Chicago's Hector Santiago (3-5, 3.59), the Orioles will start Zach Britton on Thursday and push Miguel Gonzalez to Friday in New York against the Yankees. ... Baltimore's Wei-Yin Chen will make a rehab start Thursday for Double-A Bowie and the team hopes he will get to 90 pitches. Chen has been on the disabled list since May 14 with a strained right oblique.