Novak Djokovic won a record-breaking 29th Masters 1000 title as he beat Andy Murray 6-2 3-6 6-3 in a thrilling clash at the Madrid Open final.