The now World Number Two entered the final round with a one-shot lead but on a dramatic day in high winds at The Renaissance Club, it was home hero Robert MacIntyre who threatened to seal a fairytale win.
The Scot played a stunning approach to the last to card a 64 and lead by one at 14 under but four-time Major Championship winner McIlroy never knows when he is beaten and he put a tee-shot to five feet on the 17th to share the lead.
The Northern Irishman then produced some closing magic of his own, putting his approach to 11 feet and nervelessly holing his putt to sign for a 68 and seal a remarkable victory.
McIlroy's first victory on Scottish soil now goes alongside the Hero Dubai Desert Classic title he claimed in January in his Rolex Series collection and takes his tally of DP World Tour victories to 16.
He also becomes the first player to win the Scottish Open, Irish Open and Open Championship the week before he goes in search of a fifth Major at Royal Liverpool, the scene of his Claret Jug win in 2014.
"It's amazing," he said. "For the Race to Dubai, for the FedEx Cup, for my confidence in general, but for all those things that I talked about at the start of the week and trying to win the title on both sides of the Atlantic again this year, this, will go a long way.
"This is right up there with the best of (my performances), especially after missing the short birdie putt on 16. I thought if I can birdie one of the last two and get into a play-off, that would be a bonus.
"To finish two-three in these conditions, the two iron shots that I hit, the five iron into 17 and the two iron into the last are probably two of the best shots I've hit all year, and then to finish them off with the putts as well.
"It feels absolutely amazing. I've had a few close calls recently so to get over the line and get this bit of confidence going into next week and the rest of the season is huge."
When play started early due to the expected high winds, overnight leader McIlroy saw a chip return to his feet on the second but he hit back with two putts from just off the green on the par-five third.
A three-putt at the fifth was cancelled out by a smart up-and-down after a 427-yard drive on the seventh but when the Race to Dubai Rankings in Partnership with Rolex leader came up short of the green at the eighth for another bogey, he was in a three-way tie with the surging MacIntyre and Tyrrell Hatton.
Hatton had holed a 26-footer at the first and hit smart approaches into the fourth and fifth before holing from 15 feet on the seventh.
He bogeyed the par-five tenth after finding the long grass off the tee but hit back from 20 feet on the next to get back to 12 under.
MacIntyre had got up and down on the third and put his tee-shot to five feet at the sixth before he hit a brilliant second into the tenth, using the contours to send his ball to six feet and setting up an eagle.
Hatton edged in front on his own with a tee-shot to seven feet at the par-three 12th but he dropped shots at the 13th and 16th and MacIntyre took advantage.
The 26-year-old produced a big fist-pumping celebration as he holed from 11 feet on the 14th and when he put his tee-shot inside a foot on the next, he led by two.
McIlroy had dropped another shot on the ninth after missing the green off the tee but a stunning second to four feet on the 11th meant he was the nearest challenger.
He was back in a tie as he holed a 38-footer on the 14th and MacIntyre dropped a shot on the 16th but moments later the home favourite would hit the shot of his life.
A ragged tee-shot had found its way to the left rough on the last but MacIntyre hit an incredible wood to three feet for a closing birdie before McIlroy produced his own big finish.
MacIntyre and McIlroy embraced after the drama was over and for the man from Oban there is the consolation of winning the Jock MacVicar Memorial Award as leading Scot.
World Number One Scottie Scheffler, South Korean Byeong-hun An and Swede David Lingmerth finished in a tie for third at ten under, with An, Lingmerth and Dane Nicolai Højgaard - who finished a shot further back - all earning spots in the field at next week's Open Championship.
Lingmerth closed with a 66 and Scheffler and An both carded 70s to finish a shot ahead of Hatton - who double-bogeyed the last - Højgaard, Tommy Fleetwood, Tom Kim and JT Poston.