Kokkinakis, ranked 175th in the world, needed a wild card just to get into qualifying. But he showed nerves of steel in a 3-6, 6-3, 7-6 (7/4) triumph over 20-time Grand Slam champion Federer.
"I was calmer than I thought," Kokkinakis said of the third-set tiebreaker. "Inside I was pretty happy, I was excited, but I kept pretty calm."
Federer, who won his 20th Grand Slam crown at the Australian Open, regained the number one spot in February after winning the ATP title in Rotterdam but needed to reach the quarter-finals to remain at the summit.
Instead he'll be replaced by Spain's Rafael Nadal when the rankings are released at the end of the tournament.
"I deserve it after this match. That's how I feel. Just so bad," said Federer, adding that he "could never get to any level that I was happy with today".
"Sometimes you have these matches," he said. "Sometimes you find a way through. I just couldn't get it done today."
The 36-year-old wasted no time in announcing he'll skip the clay-court season, including the French Open, just as he did last year after winning titles in Indian Wells and Miami.
The 21-year-old Kokkinakis, who has already battled injuries in his young career, became the lowest-ranked player to beat a world number one since Spain's Francisco Clavet was 178th when he upset top-ranked Lleyton Hewitt in the second round at Miami in 2003.
Federer appeared firmly in control when he breezed through the first set without facing a break point, dropping just six points on his own serve. His lone break for a 3-1 lead was all he needed to pocket the set.
But after saving break points in the opening game of the second, Kokkinakis seized upon a lackluster service game from Federer to break for a 3-1 lead, holding firm on his own serve to force the decider.
"I feel like it already started beginning of the second set when I had break points first game, and he almost wanted to give me that game, and I couldn't take it," Federer said. "From that moment on, I played maybe a bad 10 minutes, which cost me the second set, maybe the match."
Kokkinakis admitted he "didn't really put any pressure" on Federer in the opening set.
"In the second set I kind of used a couple of loose errors – I started getting in the rallies more and dictating a bit with my forehand."
Federer was again impenetrable on his serve in the third, but he was unable to capitalize on his only two break points, both in a marathon sixth game, as they went on serve to the tiebreaker.
Two errors from Federer gave Kokkinakis a 4-3 lead. A stinging forehand winner put the young Aussie up 6-4, and he closed it out when Federer netted a backhand service return.
"When I feel like I'm playing on my terms I don't think there's too many people that can go with me Kokkinakis said. "I just needed to play my game and play aggressive tennis."
Kokkinakis was delighted to beat a player he so admires, calling Federer "such a good role model for the sport".
He might have benefited from a little inside information after practising with Federer, who invited him to his training facility in Dubai.
"I've trained with him a lot of times," Kokkinakis said. "I took what I learned from him and sort of kind of knew how he played a little bit."
Despite his own disappointment, Federer was pleased for the younger player.
"I've always liked his game," he said. "I'm happy for him that on the big stage he was able to show it, center court, Miami, people watching, beating me. It's a big result for him in his career. I hope it's going to launch him, really getting his ranking up."
Kokkinakis next faces Spanish veteran Fernando Verdasco, a 4-6, 6-0, 6-2 winner over compatriot Guillermo Garcia-Lopez.
Czech 10th seed Tomas Berdych punched his third-round ticket with a 6-1, 6-4 victory over Japan's Yoshihito Nishioka.
He'll face rising US talent Frances Tiafoe, who downed 21st-seeded Briton Kyle Edmund 7-6 (7/4), 4-6, 7-6 (7/5).