Rory McIlroy birdied the last two holes in whipping wind Sunday for a 2-under 68 to win the Genesis Scottish Open, his first victory on Scottish soil, and take a load of confidence into the final major of the year.

McIlroy was one shot behind Robert MacIntyre when he played the slope to near perfection on the par-3 17th for a 4-foot birdie putt to tie for the lead. Then, he delivered what McIlroy called his best shot of the year — a 2-iron into the wind to 10 feet for a final birdie.

It was a heartbreaker for MacIntyre, who was trying to win his national open and delivered a class shot of his own. MacIntyre hammered a 3-wood from the rough on the 18th hole at The Renaissance Club to 4 feet, pumping both fists when it dropped for a 64.

CLICK HERE FOR MORE SPORTS COVERAGE ON FOXNEWS.COM

Rory McIlroy lifts trophy

Northern Irelands Rory McIlroy lifts the trophy following day four of the Genesis Scottish Open 2023 golf tournament at The Renaissance Club, North Berwick, Britain, Sunday, July 16, 2023.  (Jane Barlow/PA via AP)

It was a remarkable closing round given the wind that was rough and relentless off the Firth of Forth, and it looked for the longest time that it would give the 26-year-old MacIntyre the signature victory of his young career.

Instead it was McIlroy, who played the tough back nine in 31 and capped it off with two clutch birdies for his first win since the Dubai Desert Classic.

The victory came at an ideal time. McIlroy heads south to Royal Liverpool for the British Open, where he tries to end his nine-year drought in the majors. McIlroy won the claret jug the last time The Open was played at Royal Liverpool in 2014.

"That was such a tough day — so tough," McIlroy said. "To play that back nine in 4-under par to win the tournament, I'm really proud of how I stuck in there. I hit some amazing shots down the stretch. It feels incredible. It's been a long six months, I feel, since I won in Dubai. I've given myself tons of chances, and hopefully this win sort of breaks the seal for me, especially going into next week."

STEPH CURRY NAILS EAGLE TO WIN AMERICAN CENTURY CELEBRITY GOLF TOURNAMENT: 'IT'S PRETTY SPECIAL'

Rory McIlroy celebrates

Rory McIlroy celebrates completing his round on day four of the Genesis Scottish Open 2023 golf tournament at The Renaissance Club, North Berwick, Britain, Sunday, July 16, 2023.  (Jane Barlow/PA via AP)

McIlroy finished at 15-under 265 and moved past Jon Rahm to No. 2 in the world.

Scottie Scheffler, the No. 1 player in the world, closed with a 70 and tied for third with Byeong Hun An (70) and David Lingmerth (68). Scheffler has finished among the top five in his last seven tournaments, two of them majors.

An and Lingmerth received a consolation prize by earning spots in the British Open, awarded to the leading three players not already exempt. The final spot went to Nicolai Hojgaard (67), who will join twin brother Rasmus at Royal Liverpool.

The Scottish crowd had been chanting MacIntyre's name all week around The Renaissance Club, and they roared when he delivered the 3-wood and birdie putt to the 18th, one of the toughest closing holes on tour.

WARRIORS’ STEPH CURRY DRILLS HOLE-IN-ONE AT CELEBRITY TOURNAMENT, SETTING OFF WILD CELEBRATION

Rory McIlroy lifts trophy

Northern Irelands Rory McIlroy lifts the trophy following day four of the Genesis Scottish Open 2023 golf tournament at The Renaissance Club, North Berwick, Britain, Sunday, July 16, 2023.  (Jane Barlow/PA via AP)

MacIntyre was so caught up in the moment he had to hold back tears leaving the green.

"I’ll never forget it. I had to take a minute coming off 18," he said. "If not The Open, the Scottish Open will be up there with the event I want to play for the rest of my life. It’s one I’ve dreamed of winning since I watched at home, and I thought today coming down once I birdied 18, I thought, this might be the one. But it’s not to be just now.

"Rory McIlroy’s potentially the best in the world, and he showed why today."

McIlroy matched him with a shot every bit as good.

He was 201 yards from the pin, dead into the wind, a perfect 3-iron for him — except that McIlroy decided to replace his 3-iron with a 2-iron for the windy week.

CLICK HERE TO GET THE FOX NEWS APP

Rory McIlroy reacts

Northern Irelands Rory McIlroy reacts after a pitch on day four of the Genesis Scottish Open 2023 golf tournament at The Renaissance Club, North Berwick, Britain, Sunday, July 16, 2023.  (Jane Barlow/PA via AP)

"The 4-iron was only getting me to the front edge," McIlroy said. He needed to hit a 2-iron with a little cut and a little height to let the wind take off some of the distance, and "it came off absolutely perfectly."

"It's probably the best shot I've hit all year," McIlroy said. "When you hit a shot like that, I feel like I deserved to hole the putt to finish it off like that."

The tournament was co-sanctioned by the European tour and the PGA Tour. McIlroy expanded his lead atop the Race to Dubai, though he still has work to do to catch up to Rahm and Scheffler in the FedEx Cup.