The Tigers staged two last-chance comebacks in the marathon contest.
They trailed 10-7 heading in the bottom of the ninth and rallied to tie it on Miguel Cabrera's three-run homer.
Then in the top of the 11th, the Red Sox scored twice on RBI from Nick Punto and Dustin Pedroia, taking a 12-10 lead.
But Detroit rallied again in the bottom of the frame off Mark Melancon (0-2) when Delmon Young drove in a run with a sacrifice fly and Avila followed by smacking a knuckle-curve ball into the right field stands with two outs.
Cabrera went 3-for-5 with five RBI, Austin Jackson added four hits with three runs scored and Jhonny Peralta stroked a three-run double for the Tigers, who swept their season-opening series from the Bosox.
Duane Below (2-0) got credit for the win after retiring one batter in the 11th.
Punto ended with three hits and three RBI. Adrian Gonzalez hit a two-run homer for the Red Sox, who have started 0-3 for the second straight season.