El Bakkali, a double Olympic and world champion, seemed on course for the hat-trick when he hit the front at the bell and after surging clear seemed to think he had it in the bag.
Beamish, however, famed for his late kick, had other ideas and pushed on to snatch it on the line in eight minutes 33.88 seconds, beating the champion by seven hundredths of a second.
It completed a remarkable week for the Kiwi after he fell with only a lap to go in his heat, getting stamped on the head in the process, but recovered to qualify.
Seventeen-year-old Kenyan Edmund Serum took a brilliant bronze in 8:34.56.
