It was the second loss in six days for the Swiss, who was beaten by Juan Martin del Potro in the Indian Wells final and is set to lose his top ranking to Rafa Nadal even though the Spaniard is not playing in Miami.
Kokkinakis, ranked 175th in the world, used a vicious top-spin forehand to wear down the defending champion and claim an upset victory in front of a stunned near-capacity crowd.
"Honestly, in the third set anything could have happened," Federer told reporters.
"The ball, I wasn't feeling. With my movement, things weren't absolutely working.
"I felt like the third could be a tricky one, of course. I feel like every time I had chances, something bad happened for me, wrong decision making by me, good decision making by him.
"Who knows what happened? It just felt like I could be paying the price for opportunities missed."
Kokkinakis was ecstatic after beating Federer at the first attempt.
"This is nuts," he told reporters.
"I've trained with him a lot of times. I took what I learned from him and sort of kinda of knew how he played a little bit and just played my game and executed."
The match turned in the second set.
"I finally used a couple of loose errors," the Aussie said. "I started getting in the rallies a little bit more and dictating with my forehand.
"When I feel like I am playing on my terms, I don't think there's too many people who can go with me."
Kokkinakis, 21, born in Australia to Greek parents, only got into the Miami qualifiers on a wildcard but he displayed the talent that has long had him earmarked as one to watch, improving the longer the match progressed.
Federer looked to be taking care of business as usual when he broke Kokkinakis in the fourth game of the opening set.
There were few danger signs for 20-times grand slam champion Federer until he was broken to love in the fourth game of the second set as his first serve deserted him.
Sensing vulnerability, Kokkinakis pounced and the whole tone of the match shifted.
He won the set on serve to cursory applause and the decider was finely balanced throughout.
Kokkinakis displayed poise beyond his years to go toe-to-toe with the 36-year-old Federer and dominated the tiebreak, winning on a second serve that Federer sent into the net.
Kokkinakis let out a scream as the small Australian contingent in the crowd celebrated, leaving Federer to walk off the Crandon Park court for the final time before the event moves to a new venue next year.
"It's disappointing," added Federer.
"Don't know why I could never get to any level that I was happy with today. Sometimes you have these matches. Sometimes you find a way through. I just couldn't get it done today."