Lopez was one of the main aggressors of the 97 kilometre climbing intensive stage, attacking on the Col de Beixalis with just under 40km to go. He was brought back, but then went again on the descent and was joined by Nairo Quintana (Movistar).
He was eventually joined by Mas and race leader Simon Yates (Mitchelton-Scott) when the latter attacked from the remnants of the peloton to bridge the gap on the penultimate climb.
Mas and Lopez worked with Yates to put themselves on the overall podium, eventually dropping the British rider as they journeyed up to the summit of the Coll De la Gallina.
Mas took out the final kick to the line with a well-timed surge, celebrating in style as he crossed the line. Mas revealed that he had copied one of his rivals in the race, formerly the second-placed rider on GC, Alejandro Valverde (Movistar).
"I watched last week the video of the 2012 stage that concluded atop Gallina that Valverde won," said Mas, "so I knew I had to be at the front before the last bend and that's why I kept the speed high and accelerated 150 metres from the line. Taking my first Grand Tour stage win is something beautiful which I will always remember."
Mas, 23, came into the race as a promising youngster, but has really put forward his name as a star of the present as well as the future with his performance in his home nation.
"I can't tell you how happy I am to be in the top three," said Mas, 'but at the moment I still don't want to think about this, because there's still one day to go and only in Madrid I will truly savour this achievement. It's the first time that I am in this position and hopefully it won't be the last time."
Mas jumped from fourth to second with his performance, overtaking Valverde and Steven Kruijswijk (Lotto NL-Jumbo) who weren't able to follow the attacks of the younger riders.
"After the penultimate weekend," said Mas, "I began thinking I could finish in the top five or even on the podium, and helped by a great Quick-Step Floors team, I took it day by day and continued to rise in the GC, culminating with this beautiful result, for which I worked hard and made a lot of sacrifices, that today paid off."
Lopez, 24, was the other big beneficiary of the day, vaulting his way from fifth up to third overall, and taking his third second-place finish of the race.
"It was all or nothing for me today as I wanted to reach the podium and it was my only goal today," said Lopez. "Our tactics for today was to do all possible to step on the podium. I did all I could in this stage and I think we can be more than happy. I want to thank my team for a huge support today and during all this race.
"On the final climb I pushed as hard as I could, I did not think about the stage win because if I would begin to play games I could miss the GC podium. Of course when we came to the finish I wanted to win a stage as well, I missed just a little in the final, but I gave my all, so I don't have anything to regret about."
It will be Lopez's second Grand Tour podium of the season, after he took third at the Giro d'Italia with a late surge up the standings.
"My team and I did our best and we can be proud. We still have one stage to go in Madrid and only after the finish I will believe in my second GC podium in this season."
Together with Simon Yates, 26, the podium is entirely made up of younger riders, representing a generational shift in the professional peloton, with riders like Alberto Contador and Joaquim Rodriguez retired and ageing stars like Valverde, Vincenzo Nibali, Geraint Thomas and Chris Froome all well into their thirties.