The 23-year-old powered away from Dibaba with four laps to go and led home an Ethiopian podium sweep in a championship record time of 14 minutes 26.
Dibaba, who was hoping to become the first woman to win both the 1,500m and 5,000m at the same world championships or Olympics, was pipped at the line by compatriot Senbere Teferi, who took silver in 14.44.07.
“I had to win the gold medal,” Ayana told reporters. “It was a hard race, a hard competition in general. It was great for our country that we won gold, silver and bronze.”
The 24-year-old Dibaba, who was almost as dominant in the 1,500m on Tuesday as Ayana was in the 5,000m, went with her compatriot when she initially broke to the front of the field with seven laps to go.
After aggravating an injury during her 1,500m triumph, however, she was unable to stand the pace when Ayana moved up another gear.
To add insult to injury, the championship record Ayana bettered was set by bronze medallist Dibaba’s sister Tirunesh at the 2005 world championships.
“My country won three medals, I can only be pleased about this,” she said.
“It was a really hard race. I had so many races recently and after the 1,500 metres final my injury on the left foot started. I have a heel spur which hurts a lot. The race was hard because of this injury.”
Dibaba is the cousin of twice Olympic champion Derartu Tulu, while triple Olympic champion Tirunesh won the 5,000-10,000 double at the Bird’s Nest Stadium at the 2008 Olympics.
The defeat will not do much to tarnish her breakthrough season, during which she shattered the long-standing 1,500m record, and offers the prospect of an intriguing family clash at the Rio de Janeiro Olympics.
Tirunesh, who took this season off to have a baby, won the 5,000m at the 2003 and 2005 world championships.
(Editing by Ed Osmond)