Tennis: Miami Open
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.
"This is nuts," Kokkinakis told reporters after beating Federer at the first attempt.
"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.
(Reporting by Andrew Both in Cary, North Carolina, editing by Ed Osmond/Peter Rutherford)