Valencia will play 2011 winners Real Madrid or Celta Vigo for a place in the semi-finals, with Celta holding a 2-1 advantage from last month's last 16, first leg ahead of their visit to the Bernabeu on Wednesday.
Malaga are facing a probable last-eight clash with holders Barcelona, who host Cordoba on Thursday having won their first leg 2-0 at the second-division Andalusian club.
Valencia went into their second leg against Pamplona-based Osasuna with a 2-0 lead and were cruising at a half-empty Mestalla when Tino Costa smashed in a free-kick to open the scoring in the 34th minute.
However, Joseba Llorente pulled a goal back with a powerful low drive three minutes later and both sides squandered a number of chances before substitute Roberto Soldado struck with the last kick of the game for a 2-1 victory that sent Valencia through 4-1 on aggregate.
In the earlier kick-off at Malaga's Rosaleda stadium, Argentine midfielder Diego Buonanotte saved the Andalusian club from potential humiliation when he scored twice and created another in a 4-1 comeback win against lowly Eibar.
Lying second in their regional section of the Segunda B division and never having played in the top flight, Eibar knocked out 2012 finalists Athletic Bilbao in the previous round and held Malaga to a 1-1 draw at their tiny Ipurua stadium in the Basque Country in the first leg.
They took a surprise lead in the 12th minute of the return game when Ruben Arroyo was quickest to the rebound from a long-range Mikel Arruabarrena effort and goalkeeper Carlos Kameni could only deflect his weak shot into the net.
With Malaga fielding a weakened side, Eibar held on relatively comfortably until the 74th minute when Buonanotte exchanged passes with Javier Saviola and struck the ball high past Eibar keeper Xabi Iruretagoiena.
Two minutes later, Buonanotte set up Seba Fernandez to make it 2-1 on the night and substitute Francisco Portillo glanced a header into the net from a Nacho Monreal centre in the 82nd minute as the visitors' defences crumbled.
Their misery was complete when Guillermo Roldan was shown a straight red card four minutes from time and Buonanotte scored his second in added time to make it 5-2 on aggregate and set up a quarter-final meeting with holders Barcelona or Cordoba.
"Nobody thought it was going to be easy," Malaga coach Manuel Pellegrini said.
"They are a team with a lot of confidence and they are having a great season," the Chilean added.
"Above all in the final 20 minutes we made the most of the space to win the game. It was tough but there was a maturity and patience there to achieve the result."
Sevilla are virtually assured of a place in the last eight after they won their first leg at Real Mallorca 5-0 and host the Balearic Islanders on Wednesday before Real Zaragoza seek to maintain a 1-0 advantage at home to Levante.
Atletico Madrid have a 3-0 advantage over Getafe when they play their second leg on Thursday, while Real Betis and Las Palmas, another second-division side, drew 1-1 in their first leg and also meet on Thursday.