Germany's Jan-Lennard Struff and Tim Puetz pulled off a shock 6-3, 6-4, 3-6, 6-7(4), 7-5 win over Spain's Feliciano Lopez and Marc Lopez to give Germany a 2-1 lead in their Davis Cup quarter-final on Saturday.
Germany raced into a two-set lead but with world number one Rafael Nadal roaring on his team mates, Spain fought back to force a fifth set at a packed Plaza de Toros in Valencia.
Spain squandered five break-points in the eighth game of the final set and they paid the price for that miss when Marc Lopez, drafted in to replace an injured Pablo Carreno Busta, was broken three games later.
Germany pounced on their chance and served out to win the marathon in four hours 23 minutes.
On Sunday world number four Alexander Zverev could secure Germany's spot in the semi-finals if he is able to snap Nadal's record 23-match winning streak in the men's team competition.
David Ferrer will play either Philipp Kohlschreiber or Struff in the final rubber.
Holders France took a 2-1 lead in their tie with Italy as Pierre-Hugues Herbert Nicolas Mahut comfortably beat Simone Bolelli and Fabio Fognini 6-4, 6-3, 6-1.
Ivan Dodig and Nikola Mektic overcame Kazakhstan’s Timur Khabibulin and Aleksandr Nedovyesov 6-7(2), 6-4, 6-4, 6-2 to hand Croatia a 2-1 lead.
The United States could become the first team to seal their place in the semis later on Saturday if Jack Sock and Ryan Harrison beat Belgians Sander Gille and Joran Vliegen in Nashville.