Leading off the bottom of the seventh inning in a 2-2 tie, Castillo launched a 2-0 pitch from Tigers reliever Joe Jimenez over the right-field wall for his 11th home run of the season.
Beckham then followed in the bottom of the eighth by launching the 10,000th home run in Orioles' history deep into the left-field stands. Adam Jones also homered for the Orioles (54-56), who are the fourth major league franchise to reach that milestone.
Jonathan Schoop added an RBI double for Baltimore, which earned its first victory in this four-game series and snapped the Tigers' four-game winning streak.
Starting pitcher Wade Miley settled down after a rocky beginning before giving way to the bullpen, which shut the door with four scoreless innings.
Brad Brach pitched 1 2/3 innings of scoreless relief to earn his third win of the season, and closer Zach Britton extended his American League record by converting his 56th straight save opportunity.
After falling behind 2-0, Jones jump-started the Orioles' attack in the third inning by ripping a one-out double to left-center field off of Tigers starter Drew VerHagen.
Jones advanced to third on a sacrifice fly by Manny Machado and then scored on a double to nearly the identical spot in left-center by Schoop, slicing the Orioles' deficit to 2-1.
With one out in the bottom of the fifth, Jones tied the game when he sent a 1-0 pitch from VerHagen into the left-field stands for his 20th home run of the season.
Jones finished 2-for-4 with an RBI and a run scored. He set the stage for the bullpen and Castillo to bring home the win.
The Tigers fell to 51-58 overall. Their only runs came courtesy of a two-run homer to left in the first inning by Justin Upton.
Upton, who hit the game-winning grand slam Friday night, hammered an 0-2 pitch from Miley over the left-field wall with one out in the first. It was Upton's 21st home run of the season and marked the first time he had homered in back-to-back games since May 2014.
Making his 23rd start of the season, Miley settled down after his rocky first inning that saw four consecutive Tigers reach base.
Miley (5-9) threw 113 pitches to get through his five innings of work. He allowed two runs on four hits and came away with his second no-decision in his last four starts.
NOTES: RHP Chris Tillman has lost his spot in the Orioles' starting rotation. Manager Buck Showalter said Tillman will be available out of the bullpen for Sunday afternoon's series finale. Tillman is 1-7 with an 8.10 ERA in 15 starts this season. He has not one since his first start of the season on May 7. ... SS Tim Beckham homered in three consecutive games for the first time in his career. He has multiple hits in each of his first five games with the Orioles since being acquired from Tampa Bay on July 31. ... The Orioles are 2-4 against the Tigers this season heading into Sunday afternoon's series finale.