Trailing 2-0 after dropping both singles matches on Friday, Spain were forced to go the distance on the Vancouver hardcourt before they nervously claimed a 4-6 6-4 6-7 6-3 6-2 victory.
Daniel Nestor, an Olympic gold medalist and 13-time Grand Slam winner, and partner Vasek Pospisil appeared ready to send Canada through to the last eight for the first time as the hosts took a two sets to one lead.
But Granollers and Lopez kept up the pressure as the Canadians slowly began to run of steam.
When the fading Canadians double-faulted to hand Spain the early break and a 2-1 lead in the fifth set the five-time Davis Cup champions never looked back, completing victory in just under four hours.
The top ranked Spain, missing top-15 players Rafa Nadal, David Ferrer and Nicolas Almagro and world number 24 Fernando Verdasco, still need to win both of Sunday's reverse singles to avoid their first loss in the first round since 2006.
- Sports & Recreation
- Vasek Pospisil
- Daniel Nestor