Hanson finished at 21-under 267 on The Masters Course at Lake Malaren and earned $1,166,600 in the European Tour's richest regular event. He won the KLM Open last month in the Netherlands and has six career tour victories.
"It's fantastic to win the BMW Masters, and to go head-to-head with the world No. 1 is always nice, but to come out on top, it feels great," Hanson said. "This is by far my biggest win, so it feels great, as we had just so many guys in the top 10 in the world, so this trophy is very, very special to me."
The top-ranked McIlroy, the winner last year when the event was a non-sanctioned exhibition tournament, finished with a 67.
"I didn't get the best of starts but it started coming together over the back nine," McIlroy said. "But he played well and deserved his win. To go out there with the lead and play the way he did and shoot 67, is very impressive, so he was always going to be tough to beat."
McIlroy, who began the final round one shot behind Hanson, fell four shots behind after five holes.
The Northern Irishman regrouped with four birdies over his next eight holes and eagled the par-5 15th. McIlroy then closed within two when he holed a 25-footer for birdie at the par-3 16th.