Chris Tillman gave up just one hit over eight innings to help keep the Orioles within one game of the New York Yankees in the American League East with five games remaining.
Boston took a 1-0 led in the top of the first and was then overwhelmed in the bottom half of the inning.
The Orioles batted around with Chris Davis hitting a two-run home run and Flaherty blasting his grand slam, the first of his career.
Flaherty also had an RBI double during Baltimore's three-run fifth and he then scored on a single by Taylor Teagarden.
Tillman (9-2) gave up a bunt single to Scott Podsednik leading off the first and Podsednik later scored on a sacrifice fly. The Boston starter allowed just two walks after that and struck out four.
All six of the first-inning runs were charged to Aaron Cook (4-11), who retired only three batters.
Puzzle-maker slips 'Murdoch Is Evil' into Rupert Murdoch's Sunday Telegraph
N.J. man wakes up from 10-hour sleep with knife in back