Federer rallied from a break down and a 5-3 deficit in the third set to force the tiebreaker, but he made a slew of mistakes to lose the 33rd meeting between the rivals.
Federer still leads the series 17-16, having beaten Djokovic in three sets in the semifinals at Dubai two weeks ago. Djokovic will remain No. 2 in the world, while Federer will rise three spots to No. 5 on Monday in the ATP Tour rankings.
Flavia Pennetta routed injured Agnieszka Radwanska 6-2, 6-1 to win the women's title, the biggest of her career.
Federer was trying to win a record fifth title in the desert, and at 32, he would have been the oldest Masters 1000 winner since 34-year-old Andre Agassi won at Cincinnati in 2004.
But Djokovic wouldn't allow it.
After Federer breezed to the first set in 31 minutes, Djokovic settled down and locked into the punishing rallies that marked most of the match. He earned the lone break of the second set to go up 5-3 after Federer pulled a forehand wide.
Djokovic got the early break in the third, again on one of Federer's forehand errors, to lead 2-1. His 112-mph ace gave him a 4-2 lead, and he extended it to 5-3 with a backhand winner down the line.
Federer served a love-game to trail 5-4 and then broke Djokovic on a forehand error to tie the set, 5-5. Federer held at love again and Djokovic gave up just one point on his serve for a 6-all tie.
The Serb raced to a 5-1 lead in the tiebreaker, helped by four errors from Federer. Another of Federer's forehand errors gave Djokovic a 6-2 lead before the Swiss star's ace ensured Djokovic would have to win the match on his serve. He did, when Federer's backhand landed in the net.
Djokovic's victory evened their rivalry on hard courts at 13 wins apiece.
A year ago, Pennetta was close to retiring from tennis.
Her ranking plunged to 166th in the world in June, and she had struggled to rebuild her career after having right wrist surgery the previous August.
Now she's glad she stuck around.
"After so many years, so much work and everything, this is the moment I always waited for," she said. "And it's coming when you don't expect. It was something I was waiting since long time, and finally I have a good trophy in my hands."
Actually, the crystal trophy was so heavy, she didn't hoist it for photographers.
The 32-year-old Italian became the third-oldest winner of the desert tournament after Martina Navratilova, who won at age 33 and again at 34 in 1990-91. It was Pennetta's first title since winning at Marbella in 2010.
"We are old, but we still good athletes," Pennetta said. "We are strong. We have so many years on the tour, and we know how to handle the emotion and everything."
As the 20th seed, Pennetta became the lowest seed to win the title. She came in ranked 21st and is projected to rise to No. 12 in the world Monday in the WTA Tour rankings. She beat top-seeded Li Na in the semifinals on her way to winning the title worth $1 million.
Radwanska, seeded second, was bothered with a left knee injury during the final. She twice called for the trainer in the second set and had her knee heavily taped. She was in tears apologizing to the sympathetic crowd afterward, saying she was unable to run.
"I had the tape stronger and stronger every changeover," Radwanska said. "I thought that going to help, but not really. Didn't work."
Pennetta threatened from the first game of the match, when she held two break points and had one more in the third game. She finally converted in the fifth game on Radwanska's errant backhand, then broke again in the seventh game on the Pole's forehand error. Pennetta won the final five games to wrap up the first set, 6-2.
Radwanska held for a 1-0 lead in the second before she got the trainer on court and Pennetta called for her coach. Pennetta staved off a break point to hold at 1-1, the last challenge Radwanska offered before it became evident she wasn't herself.
Radwanska said she hurt her knee a few days ago in practice.
"I just didn't expect it was going to be much worse today," she said. "But when the pain is so big that nothing is working -- no painkillers, no tape -- that means it is bad."
Pennetta's lob over Radwanska, who didn't even try to go after it, helped her earn the break and a 2-1 lead. The Italian swept the last six games, with Radwanska ending the match on three consecutive errors.
She earned $500,000.