The Spaniard and Martinez played a spot of cat and mouse on the climb to the finish, Mas pulling away 50m from the finish on the final kick to the line.
The pair were part of a late move set up by Mas' team mate Remi Cavagna who attacked on the foot of the final climb and reducing the number of contenders to a more manageable group who reined in the Frenchman with two kilometres to go.
Mas now leads the race and the youth classification.
“The team did an amazing work today, protecting me and chasing the break, which at one point had a pretty big gap over the peloton," Mas said of the early four-rider move. "Thanks to the guys, I was well positioned at the start of the climb and I had Remi to launch a probing attack and make others work.
"When he was caught with some 1700 metres to go, I knew that it was up to me and that all I needed to do was attack and try to make the difference.”
The preceding three stages were ones for the sprinters, Fernando Gaviria (UAE - Stage 1), Dan McLay (EF Education First - Stage 2) and Pascal Ackermann (Bora-hansgrohe) all sharing the spoils with Matteo Trentin (Mitchelton-Scott) there or thereabouts.
Two stages remain in the last WorldTour race of the season.