The defenseman shot over goalie Martin Jones' glove from the right face-off dot after taking a cross-ice pass from William Nylander, who had pulled up on the rush and turned back toward the blue line.
"It was a great play by Willie," Rielly said. "But we were motivated, we wanted to come out and have a good effort. When you look back at our past couple of games, there's room for improvement. We're aware, and we talked about that. I thought we came in, had a good attitude this morning, had lots of energy, and it carried over."
[WATCH: All Sharks vs. Maple Leafs highlights]
Defenseman Jake Muzzin scored for Toronto from the top of the crease off a pass from Jason Spezza to tie the game 1-1 with three seconds left in the second period.
"I figured why not go to the net," Muzzin said. "There was five seconds left, so even if they got the puck, it would be tough to get down the ice and score. It was kind of lucky, I guess, but I think it was a big goal for the team to get us going."
Video: SJS@TOR: Muzzin ties the game before the buzzer
Ilya Mikheyev and Auston Matthews scored in the final minute, Mitchell Marner had two assists, and Frederik Andersen made 16 saves for the Maple Leafs (6-4-2), who won for the first time in three games.
Jones made 23 saves for the Sharks (4-6-1), who were coming off a 4-2 win at the Montreal Canadiens on Thursday but lost for the third time in their past four games.
"It was tough to generate shots tonight," San Jose captain Logan Couture said. "It was a back-to-back for us, and I don't know if we had great legs, but I thought we put ourselves in a position to win a game. Obviously too many penalties, but our kill was great, kept us in it, and Jones made some big saves when we had breakdowns. We were close to pulling out a point there; it's tough we couldn't get one."
The Maple Leafs were 0-for-5 on the power play, and the Sharks went 0-for-1.
Mikheyev, who had an assist on Muzzin's goal, scored into an empty net with 48 seconds left to make it 3-1, and Matthews made it 4-1 with 30 seconds left.
Kevin Labanc gave San Jose a 1-0 lead at 10:35 of the first period when he deflected a point shot from Brent Burns in the slot.
Video: SJS@TOR: Labanc deflects Burns' shot for goal
It was forward Patrick Marleau's 1,500th game for the Sharks, making him the seventh player in NHL history to play at least that many for the same franchise. He joined Gordie Howe (Detroit Red Wings, 1,687), Nicklas Lidstrom (Red Wings, 1,564), Alex Delvecchio (Red Wings, 1,550), Shane Doan (Winnipeg Jets/Arizona Coyotes, 1,540), Ray Bourque (Boston Bruins, 1,518) and Steve Yzerman (Red Wings, 1,514).
Marleau played the past two seasons for Toronto before returning to San Jose on a one-year contract he signed Oct. 9.
Video: SJS@TOR: Matthews goes five-hole on Jones
They said it
"That was a tough one to give up at the end of the second. Then we took a penalty on the first shift of the third, and any momentum we were going to get kind of died with that sequence of play. It was tough to regain it back, and they didn't give us much." -- Sharks captain Logan Couture
"We were better in the neutral zone and we played in their zone a little bit more. A lot of times they were just flicking pucks out because they were a little bit tired. It was a tough schedule for them being on the second end of a back-to-back, which is always tough. We knew we had the jump on them and had to continue that for 60 minutes. We did a good job tonight." -- Maple Leafs defenseman Jake Muzzin
"What I liked about the game is when we had a turnover and they scored, but instead of getting crazy, we just stayed with it, stayed with it and didn't give up much tonight, which was positive. We got the break there at the end of the second period and stayed patient in the third too." -- Maple Leafs coach Mike Babcock
Need to know
Labanc, who scored against Montreal, has a three-game point streak (two goals, one assist). ... Muzzin has one goal and four assists in his past five games. ... Rielly has scored three goals in the past four games after he had none in the first eight of the season. ... Goalie Michael Hutchinson is expected to start for the Maple Leafs against the Canadiens on Saturday.
Sharks: At the Ottawa Senators on Sunday (7 p.m. ET; ESPN+, SN360, SN1, TVAS, NBCSCA, NHL.TV)
Maple Leafs: At the Montreal Canadiens on Saturday (7 p.m. ET; CBC, SN1, CITY, TVAS, NHL.TV)
Video: Rielly breaks tie in 3rd, Maple Leafs top Sharks