Hanson opened the final round with a one-stroke edge over McIlroy and had a 5-under-par 67 Sunday that was just enough to stay ahead of McIlroy, who also turned in a 67. He totaled 21-under 267 for the tournament. McIlroy was 20-under.
Luke Donald shot a final-round 66 and took third place at 17-under while Ian Poulter, with the round of the day at 7-under 65, climbed into fourth at 16-under. Shane Lawry had a 68 Sunday and was fifth another stroke back.
Hanson and McIlroy were paired together for the second consecutive day and Hanson pulled away due to a birdie at the second hole and bogeys at Nos. 4 and 5 by McIlroy.
The lead was four strokes going into the 15th, which Hanson birdied, but McIlroy had an eagle at the par-5 hole and came back with a birdie at 16. Hanson was in a bunker at 16 but saved par to keep a two-stroke edge.
McIlroy had pars at 17 and 18 and Hanson survived a plugged lie in the bank of a bunker at 18 for a two-putt bogey and the win.
Hanson had birdies at the par-5 13th and 15th for the fourth straight round. He was 13-under on the pars 5s for the week.
The BMW Masters is the sixth European Tour title for Hanson, who also won the KLM Open earlier this year.
Wisconsin business offering 'therapeutic cuddling' forced to close
Exploding whale video goes viral on Internet