The Serbian overcame a shaky start when he dropped his opening service game, then survived a nervous finish when he failed to serve out the match at the first attempt before finally sealing victory in the tiebreaker.
"Today was an incredible match," Djokovic said after being presented with the winner's crystal trophy.
"It was an incredibly difficult match. Roger is playing great and it's always a pleasure playing with him."
The thrilling victory gave Djokovic his third championship in the Californian desert tournament and lifted his career tally to 42 titles.
It was Federer's first loss in a final at Indian Wells after he had previously won a record four times.
But the 32-year-old showed he was anything but a spent force as he went toe to toe with the best hardcourt player in the world.
"Personally, I'm very happy. I think I'm playing great tennis now and I'm really enjoying myself," Federer said.
"Of course, I would have liked to have more won a few more points at the end but Novak made it tough, so congratulations to him for winning."
Federer reached the final without dropping a set and made a flying start against Djokovic, in their 33rd career meeting. Despite Sunday's loss, Federer leads their head to head record 17-16.
Djokovic double-faulted twice in his opening service game as his Swiss opponent snatched the early break and went on to take the first set in 31 minutes.
Djokovic made a much better start to the second set, winning the opening point with an ace right down the middle, then broke Federer's serve in the eighth game to lead 5-3.
He served out the set then broke Federer early in the third and had the chance to serve out the match when he led 5-4.
But with Federer chipping and charging from the baseline, Djokovic cracked.
Roared on by the crowd, Federer tied the set at 6-6 to force a tiebreak, but Djokovic regained his composure to clinch the title after almost two and a quarter hours.
- Sports & Recreation