Town took the lead just before the half hour mark when Jamie McAllister swept home from just inside the area.
Cardiff equalised early in the second half when Jordan Mutch's free kick from 25 yards sailed into the top corner, leaving Dunn with no chance.
The Glovers completed the scoring ten minutes later. James Hayter found himself in space at the back post and made no mistake is heading home.