Del Potro saved three match points in the deciding set in Indian Wells as the Swiss great failed to serve out for his 18th consecutive win at the start of 2018.
The 36-year-old Federer won his 20th grand slam title at the Australian Open in January and also triumphed in the Netherlands for his 97th ATP Tour victory last month.
"I'm so happy to win a title like this," Del Potro told the ATP website, dedicating his maiden Masters 1000 victory to his beloved dog Cesar who recently passed away.
"I lost many finals in Masters 1000s but today was the day for me. I played unbelievable tennis, almost three hours, and beating Roger for the first time this year, that means a lot to me."
'A lot to celebrate'
The 29-year-old Del Potro, who beat Federer in the US Open quarterfinal last September, also won in Mexico earlier this month as he continues his rehabilitation from a series of injuries.
Del Potro has struggled with injury since beating Federer in the 2009 US Open final to win his first grand slam, missing almost the entire 2010 season after undergoing surgery on his wrist.
After slipping outside the world's top 400, Del Potro returned to the top five in 2013 but another wrist injury meant more surgery and led him to miss the majority of the 2014 and 2015 season.
"I cannot believe this moment," he said of his BNP Paribas Open victory. "I always think about what I did in the past, trying to fix my wrist problems.
"I did, I'm playing tennis again and winning titles so I have a lot of things to celebrate and this trophy is one of them."
When asked what he expected of himself for the remainder of 2018, he said: "I don't know, I'm just going my way and I'm very pleased with my level of tennis.
"I'm excited to keep surprising myself, the tour and seeing what I can do."
It was the Argentine's trademark booming forehand which helped him gain control in a tight first set, eventually winning 6-4 in 34 minutes.
The second set was even tighter and at times sprinkled with the kind of quality that graced matches between the pair a decade ago.
A Federer drop shot, expertly chased down by Del Potro, was followed by a lob and then a hotdog from the Argentine, before two more volleys from the 36-year-old brought an end to a sublime point and brought the crowd to its feet.
With nothing to separate the two after 12 games, the set went to a tiebreak and Federer celebrated victory at 7-5 as Del Potro's return of serve sailed long.
His celebrations, however, were short lived. A challenge from Del Potro showed the Swiss' serve had landed out, prompting a frustrated Federer to vent at the chair umpire after his wild second serve landed more than a foot long.
It was then Del Potro's turn to rue a missed opportunity, dumping his usually reliable forehand into the net as he went for the winner on match point.
And Federer made him pay for his error, winning the two following points to take the match to a third and deciding set.
With Federer 40-15 up on serve and leading 5-4 having broken Del Potro in the previous game, it was again the Argentine's huge forehand which began dictating the points, leveling the score at deuce.
He saved a third match point, softly stroking a passing shot down the line, before a crushing forehand winner had the usually elegant Federer scrambling desperately across his baseline.
Del Potro went on to dominate the deciding tiebreak, winning 7-2 to claim his maiden Masters 1000 title.
Federer was frustrated to throw away three match points on serve, though he will have to lick his wounds and move on quickly with another Masters 1000 event in Miami starting Wednesday.
Federer is the defending champion there, too.
"I think staying positive through the tough moments is really key," he told the ATP. "Because you're always going to go through ups and downs in your career, or as a person for that matter. Not every day is sun shining.
"I feel frustrated, you know, that I let an opportunity like this go by.
"Serving 40-15, any game I probably win -- I don't know what the stat is -- 90-something per cent.
"So it should sting, like you said, for a bit. The question is how long? It won't be long, but it's disappointing talking about a great match like this, losing, even though I was right there.
"Obviously there is not too much time to dwell over it. Like I said, I'm happy for Juan Martin. It's a tough one. And I still had a good week here. I still see the positives at the end of the day."