McAvoy's fourth goal of the season came with 1:01 left in overtime.
After blowing an early three-goal lead, the Bruins trailed 5-4 with 1:49 left in the game when Chris Wagner tied it with his eighth goal of the season.
The Bruins won their season-best sixth straight game and extended their point streak to 11 games (8-0-3).
Boston moved to three points ahead of Toronto for second place in the Atlantic Division.
The Sharks lost despite Joe Thornton's fifth career hat trick. San Jose had won seven of their previous eight games and 14 of 18 going into Monday. They fell out of a tie for first place with Calgary, which defeated Arizona.
Thornton's third goal and 13th of the season broke a 4-4 tie at 13:32 of the third period.
Tuukka Rask had 38 saves for Boston. The Bruins scored three goals in the span of less than five minutes to take a big early lead.
Torey Krug got the Bruins with a power play goal (his sixth) at 13:49 of the first period.
Zdeno Chara followed it with his fourth goal and rookie Karson Kuhlman scored his first career goal with 1:34 left in the period.
Sharks goalie Martin Jones had 20 saves.
NOTES: Peter DeBoer coached his 800th career NHL game for the Sharks. C Kuhlman played on the second line in place of Peter Cehlarik, who was injured in the third period of Saturday's game in Los Angeles. Cehlarik is day to day. Bruins F David Pastrnak (left thumb) missed his third straight game. ... Boston recalled D Steven Kampfer from his conditioning loan with the Providence Bruins. ... Sharks D's Erik Karlsson and Brent Burns were on the ice after missing a morning skate earlier in the day. . The Sharks reassigned C Dylan Gambrell to the San Jose Barracuda.
Bruins: At Vegas on Wednesday.
Sharks: At Pittsburgh on Thursday.