Del Potro hit 41 winners Sunday, 29 on his forehand. It would be a forehand off the net cord, however, that clinched victory on his second match point.
Djokovic, a 12-time major champion, had more unforced errors than winners: 32-26. The Serb, who will be 33 at the 2020 Olympics, called this "one of the toughest losses in my life."
He's still alive in doubles, though, so a gold medal in Rio remains a possibility.
At 2-2 in the first-set tiebreaker, del Potro won three straight points to take control. A big forehand that Djokovic couldn't get back gave him three set points, and he converted the second when Djokovic's forehand sailed wide.
"In decisive moments, he just came up with some extraordinary tennis," Djokovic said of the player he described as "a good friend of mine."
Del Potro's Sunday did not start off so auspiciously — he was stuck in an elevator at the Olympic Village for 40 minutes before Argentine handball players freed him.
"It's very difficult because del Potro has a very important match," one of his rescuers, Gonzalo Carou, said later.
A hugely difficult match from both del Potro and Djokovic's perspectives — each had the misfortune of drawing the other in the first round.
It was a raucous atmosphere in the last match of the night on center court, where Argentine fans chanted for del Potro and Brazilians would try to drown out their rivals with cheers for Djokovic.
Djokovic started the year with talk of a Grand Slam after he won the Australian Open and French Open. But he was upset in the third round at Wimbledon by 41st-ranked Sam Querrey. The 2008 Olympic bronze medalist seemed to bounce back by winning a hard-court tuneup in Toronto before coming to Rio de Janeiro.
"He told me really kind words at the end," del Potro said, "and I appreciated that."