With the game tied 2-2 in the ninth, Valencia greeted Koji Uehara (4-1) with a lead-off triple.
Alexi Casilla came in to run and slid home with the decisive run when Wieters followed by lining a fly ball to right field.
Tommy Hunter (6-4) earned the decision and Jim Johnson finished up with his 46th save as the Orioles won a second straight and pulled within two games of American League wild-card co-leaders Texas and Tampa Bay.
Chris Davis pounded a franchise-record 51st homer of the season for Baltimore with a score-tying solo blast in the sixth.
Dustin Pedroia went deep for the AL East-leading Bosox, whose magic number to win the division crown fell to three with Tampa Bay's loss to Texas Tuesday.