The 24-year-old free transfer will join the League One outfit on July 1.
"Gary is another young player with a hunger to succeed and a desire to help take this Club to a higher level", said Dons boss Karl Robinson.
"Dundee have had one of the best defensive records in that division for the last few years and it's no coincidence that Gary has been at the heart of it.
Mackenzie added: "Playing for an English club was something I'd always wanted to do and I feel the time is right in my career to take that step. I enjoyed my time at Dundee but it's time for a new challenge."