Nakaayi passed Wilson and sprinted clear to take gold in 1:58.04.
Wilson had to settle for bronze after losing second to her training partner Raevyn Rogers with Nanyondo fourth.
Speaking to the BBC afterwards, Nakaayi said, "Oh my God. At first I couldn't believe that at 700m I was still with her [Wilson]. But I saw her pace was a little bit declining and I was feeling strong. And I knew yes, God has made it for me.
"I like the crowd, they cheered so much. They gave me a lot of morale."
Wilson fails to last home
The race panned out as expected in the early stages with Wilson going to the head of the field.
Pan American Games champion Natoya Goule moved to her shoulder at the bell and tried to put pressure on the frontrunner.
Wilson managed to fend off the Jamaican but then came more danger in the shape of the two Ugandans, Nakaayi and Nanyondo, with 150m to go.
For the first time in a race without Caster Semenya this season, the American looked in trouble and Nakaayi kicked clear to score a shock victory.
And Wilson took bronze for the second World Championships in a row as Rogers, her training partner in Philadelphia, finished strongly to move up from seventh on the last bend to second.
And when asked by the BBC about her hopes for the Tokyo 2020 Olympic Games, Nakaayi said, "I know the future is bright. What I have to do is keep on with my training pattern. We are performing."
Women's 800m world champions
2019 Halimah Nakaayi (UGA) 1:58.04
2017 Caster Semenya (RSA) 1:55.16
2015 Maryna Arzamasava (BLR) 1:58.03
2013 Eunice Sum (KEN) 1:57.38
2011 Caster Semenya (RSA) 1:56.35
2009 Caster Semenya (RSA) 1:55.45
2007 Janeth Jepkosgei (KEN) 1:56.04
2005 Zuila Calatayud (CUB) 1:58.82
2003 Maria Mutola (MOZ) 1:59.89
2001 Maria Mutola (MOZ) 1:57.17
1999 Ludmila Formanova (CZE) 1:56.68
1997 Ana Quirot (CUB) 1:57.14
1995 Ana Quirot (CUB) 1:56.11
1993 Maria Mutola (MOZ) 1:55.43
1991 Liliya Nurutdinova (URS) 1:57.50
1987 Sigrun Wodars (GDR) 1:55.26
1983 Jarmila Kratochvilova (TCH) 1:54.68 CR