Sprinter Fabio Jakobsen (Quickstep-AlphaVinyl) came into the final day holding the leader's jersey, but gave it up on the final 12 kilometre climb of the stage.
Dunbar finished top of the general classification 23 seconds ahead of Oscar Rodriguez (Movistar) in second with Samuele Battistella (Astana) in third a further five seconds back.
Jens Reynders (Sport Vlaanderen-Baloise), third overall prior to stage 5 joined Nicolas Dalla Valle (Giotti Victoria-Savini Due), Tilen Finkšt (Adria Mobil), Ádám Kristóf Karl (Hungary), Umberto Marengo (Drone Hopper-Androni Giocattoli) and Sergio Roman Martín (Caja Rural-Seguros RGA) in the day's breakaway.
The group remained in front until the peloton picked up the pace in the lead up to the final climb, and were caught as the ascent to Kekestetö began.
The big teams took up their positions in the early kilometres of the climb, with INEOS rider Ben Turner bringing Dunbar to the front.
Then with two kilometres to the finish, Dunbar took his opportunity and went clear off the front, carving out a sizeable gap as Tibari and Rodriguez gave chase behind.
Dunbar looked to have done enough to take victory inside the final few hundred metres, until Tibari ramped up the intensity to catch the Irishman just before the line and overtake him for the win.
With Tibari no threat in the GC though, Dunbar was able to claim overall honours with the second place finish.