The Mitchelton-Scott rider crossed the line in 4:19:27, with Astana Pro Team’s Miguel Angel Lopez second and Movistar Team’s Alejandro Valverde third.
The 171km stage, which started in Cistierna, featured five classified climbs and was the second consecutive summit finish.
With 49km to the finish, Lotto Soudal’s Thomas de Gendt led the break as they reached the climb that was the start of the finale.
This group also consisted of BMC Racing Team’s Brent Bookwalter, teammate Nicolas Roche, EF Education First-Drapac p/b Cannondale’s Michael Woods, Bahrain-Merida’s Ivan Garcia Cortina and Team Sky’s Michal Kwiatkowski.
They were 2:00 in front of the peloton, which included previous overall leader Jesus Herrada Lopez of Cofidis, Solutions Credits.
After taking maximum points on the climb, De Gendt suffered a mechanical and had to chase back to the leading group, which had lost Cortina and Woods in the meantime.
Bahrain-Merida’s Vincenzo Nibali led the peloton, which was 1:20 behind the leading trio.
With 26km remaining, Bookwalter was dropped by the break, which left just Roche and Kwiatkowski.
Roche started to show signs of fatigue which saw the Polish cyclist push ahead for the stage victory on his own. But the general classification riders at the front of the peloton were only 40 seconds back and chasing hard.
With 14km remaining there was a crash in the main bunch. Dimension Data’s Louis Meintjes went down and needed treatment before continuing.
Kwiatkowski was still out in front with a lead of 40 seconds as he fought to not get caught before the steep final 4km before the finish.
With 5km left the front group that consisted of 22 riders caught the leader, with Bora-Hansgrohe’s Rafał Majka launching the first attack.
LottoNL-Jumbo’s Steven Kruijswijk was next to attack when Kwiatkowski was dropped. The former was then chased by Movistar’s Richard Carapaz and teammate Valverde.
This duo was then joined by EF Education First–Drapac p/b Cannondale’s Rigoberto Urán, Quick-Step Floors’ Enric Mas, Movistar’s Nairo Quintana and Yates.
The group then surged ahead and caught Kruijswijk. Quintana then picked up speed and was followed by Lopez.
With 1km to the finish line, Yates attacked and stayed ahead to win the stage and take back the overall lead.
“It was a very difficult stage but I chose my moment really well in the very finale,” said Yates.
“I didn’t look back until it flattened off a little bit. I had a bit of a gap and so I gave it everything to the line.”