Goalie Semyon Varlamov made 30 saves, including on a breakaway against Joe Pavelski on a Sharks power play late in the third period after the Avalanche nearly gave away all of their 4-0 second-period lead.
"I shot five-hole," Pavelski said. "I shouldn't have shot five-hole. I never shoot five-hole. I saw a lot of openings, but for some reason that's where I went. If I could have it back, I would."
The Avalanche, who have won three games in a row and are 3-0-1 to start a seven-game homestand, reached the halfway point of their season in third place in the Central Division with a 26-11-4 record and 56 points.
The Sharks are second in the Pacific Division with a 26-10-6 record and 58 points, seven behind the first-place Anaheim Ducks. San Jose plays at the Chicago Blackhawks on Sunday.
"We used up a lot of energy to come within one," Sharks coach Todd McLellan said. "We're in the midst of a tough schedule, played guys down the stretch an awful lot and still came up short. Take whatever you want out of that other than we played well. We used up a lot of gas today."
Down 4-0 early in the second period, the Sharks mounted an impressive comeback and climbed within 4-3 at 5:44 of the third period when Logan Couture moved unchecked to the front of the net to knock a loose puck behind Varlamov.
"We obviously don't want to let them back in the game like that," said MacKinnon, who took over the lead among NHL rookies with 26 points. "They're a team that pushed hard. They came close, but thankfully we hung on to get the win. [Varlamov] really bailed us out, like he usually does when we get the win. It was a very intense last couple of minutes but thankfully we managed to hang on."
The Avalanche scored three goals on three shots in 66 seconds to grab a 4-0 lead in the second period, but the Sharks closed within 4-2 with two goals that came 1:36 apart.
Jamie McGinn and Erik Johnson connected against Sharks goalie Antti Niemi 52 seconds apart, with Johnson scoring from the blue line while he was along the right-wing boards. The puck bounced off Niemi into the net at 5:45, prompting McLellan to replace him with Alex Stalock.
"That's a nice break. I haven't gotten one of those in a while, so it was really nice to get a break like that," said Johnson, who has five goals and five assists in the past 10 games. "I think even when we were up 4-0 we knew the game was far from over. They're never out of a game no matter what. That's how it happens when you build a big lead. You tend to sit back."
MacKinnon scored 15 seconds later on the first shot against Stalock, using his speed to skate down right wing before beating him to the far side.
"It was 4-0 at that point, so I didn't think it would be the winner," MacKinnon said. "I didn't want it to be the winner, to be honest with you. But we hung in there."
Avalanche coach Patrick Roy was upset when San Jose's Andrew Desjardins stuck his stick between John Mitchell's legs and was assessed a slashing penalty rather than a spear on the faceoff that followed MacKinnon's goal.
"Obviously after the non-spearing call we kind of lost our focus and gave them a couple power plays, one was on a delayed [penalty]," Roy said.
Desjardins said it was not intentional.
"It was more like you come together and you try to push the hands away and my stick caught him in an unfortunate spot," he said. "Just wanted to be intense and it just happened."
The Sharks got on the board at 8:35 on a power-play goal from Pavelski, who converted a rebound into a half-open net. The U.S. Olympian has five goals and four assists in a seven-game point streak.
Patrick Marleau, who had a goal and two assists, beat Varlamov with a one-timer from the right point at 10:11 during a delayed penalty to make it 4-2.
The Sharks outshot the Avalanche 11-2 in the final 14 minutes of the second period and 10-4 in the third.
"This is a very good team, a team with lots of skill," Roy said of the Sharks. "In the third they were pressing and I thought we tried to defend a little too much. We should have tried to press more. I would have loved to see us keep putting pucks at the net."
The Sharks outshot the Avalanche 33-21 for the game, and Pavelski had a team-high seven, four in the third period.
"We started playing a little better," Pavelski said. "A little too loose early and you put yourself in a 4-0 hole and it's tough to come out of. We almost did. We need a better start."
MacKinnon scored a power-play goal at 15:28 of the first period. Andre Benoit took a shot from the left point and MacKinnon deflected the puck over Niemi's right shoulder.
McGinn's goal gave the Avalanche a 2-0 lead at 4:53 of the second period after Sharks defensemen Dan Boyle and Marc-Edouard Vlasic overskated the puck at the side of the net. McGinn gained possession and scored from just outside the crease, stretching his goal streak to three games.
"Two veteran D-men you put a lot of trust in make a mistake, they bobble it in the front of the net," McLellan said.