Oliver Wilson, who is ranked 792 in the world, held on to his nerve and his lead to triumph at the Alfred Dunhill Links Championship and land his first ever European Tour title.
The 34-year old Englishman ultimately won by one shot ahead of a three-way tie for second, which included 2014 Open champion McIlroy who finished on 16 under par.
After securing the win, an emotional Wilson said: “So many people had written me off and that hurt, but I kept believing. A lot of people had a part in this and I can’t thank them enough. This is pretty special.
“When I came here at the start of the week I was just trying to make the cut and each day my confidence grew.”
Wilson went into the final round with a three-shot lead but lost it over the front nine, and his closest challenger McIlroy got off to the worst possible start when he double-bogeyed the opening hole after seeing his ball spin back into the Swilcan Burn.
However, the world number one bounced back with four straight birdies and he was tied for the leader going into the back nine, although a bogey at the 17th cost him after his putt attempt rolled back into the bunker.
The Northern Irishman did manage to birdie the final hole, but ultimately he came up short, and the celebrations started for Wilson.
However, despite the loss, McIlory enjoyed playing with his father who was celebrating his 55th birthday.
“It was fantastic to play with my dad and for him to play St Andrews on his birthday was very special,” McIlroy said.