The lead changed hands three times and the contest was deadlocked at 7-7 when Flaherty delivered Chris Davis with the clutch base hit.
Xavier Avery hit the first home run of his career in the eighth to give the Orioles a two-run cushion and Jim Johnson survived a ninth-inning jam to pick up his 23rd save, tied for most in the majors.
Baltimore overcame an early 1-0 deficit, and then let a two-run lead get away before going back in front on a three-run homer by Matt Wieters in the sixth.
Cleveland rallied to tie it, but Flaherty's single in the seventh gave the Orioles the lead once more on their way to ending a three-game losing streak.
Matt Lindstrom (1-0) was credited with the win after throwing only three pitches while retiring the final batter in the seventh.
Joe Smith (5-2) gave up Baltimore's go-ahead run in the seventh to take the loss.