Rafael Nadal kept Spain's hopes alive, then Marcel Granollers and Feliciano López completed the comeback in the decisive doubles match to give the hosts a 2-1 win over Russia in the inaugural Davis Cup Finals.
Granollers and López defeated Karen Khachanov and Andrey Rublev 6-4, 7-6 (5) to seal the victory for Spain in a marathon series that ended close to 2 a.m. local time (0100 GMT) on Wednesday.
The top-ranked Nadal earlier defeated Khachanov 6-3, 7-6 (7) to level the series 1-1 after Rublev had rallied to beat Roberto Bautista-Agut 3-6, 6-3, 7-6 (0) in the first match.
"It was a difficult situation for us," López said. "We needed this win, otherwise we could be close to being out of the tournament. Now we are in a good position to advance."
Although only one of the matches reached three sets, it took nearly eight hours to complete the series at the "Caja Mágica" (Magic Box) center court. Spain will be back in action on Wednesday against defending champion Croatia. Russia beat Croatia on Monday.
In the revamped Davis Cup format debuting this year, teams play only two singles and a doubles in each tie, with the group winners advancing to the knockout stage along with the two best second-place finishers in the six groups.
"The format makes things very difficult because every mistake puts you in a position that you don't want to be," Nadal said. "And losing that first match, we were under pressure."
Serbia's Novak Djokovic and Britain's Andy Murray will make their first appearance on Wednesday. Serbia takes on Japan and Britain plays the Netherlands.
The Davis Cup Finals is being played in World Cup-style with all 18 teams playing in a single venue in the same week. It is the result of a 25-year partnership between the International Tennis Federation and Kosmos, a group co-founded by Barcelona soccer player Gerard Piqué.