Two goals in the second half from the experienced striker sealed a win for Vicente del Bosque's men after Cesc Fabregas had earlier missed a penaltyTwo second-half goals from David Villa secured a 2-0 win for Spain over El Salvador in their final World Cup warm-up clash on Saturday.
Spain's record goalscorer and recent New York City acquisition was required to spare his country's blushes as they were forced to work hard at FedEx Field in Maryland on Saturday.
Cesc Fabregas' penalty woes at international level continued as he spurned a spot-kick early on in what proved to be a frustrating goalless first half for the world and European champions.
However, substitute Villa opened the scoring 15 minutes into the second half when he fired home Sergio Ramos' headed knockdown into an empty net.
He then added a second two minutes from time to cap off a positive outing for Spain, who were given a stern test of their offensive capabilities.
Despite recent injury problems, Diego Costa came through 74 minutes unscathed to boost Vicente Del Bosque's side of their World Cup opener against the Netherlands on Friday.
Fabregas wasted a glorious opportunity to open the scoring from the penalty spot after just four minutes following a foul on Costa by goalkeeper Derby Carrillo inside the area.
However, the Barcelona man sidefooted his penalty over the crossbar as he missed a third spot-kick in normal time for his country.
Carrillo was busy early on - first thwarting Pedro with a good save one-on-one before keeping a speculative long-range drive from Ramos out.
Del Bosque's men continued to probe but, for all their dominance, they were frustrated by a resilient El Salvador back line.
Spain were given a scare when Jordi Alba went off for treatment following a strong challenge, although he was able to return to action soon afterwards.
Alexander Larin then had El Salvador's first attempt on goal - a free-kick from well outside Spain's penalty area that went high and wide - before Spain resumed their near-complete control of possession.
Despite plenty of intent, Spain's deft lobbed passes and slick movement failed to yield a breakthrough before the interval, with Del Bosque opting to replace Fabregas, Andres Iniesta and Pedro with Villa, Santi Cazorla and David Silva.
The latter produced a strong run shortly after the break but saw his shot blocked, before Villa was denied by Carrillo at close range.
However, the experienced Villa was on hand just before the hour to capitalise on Ramos' knockdown and finish well into an open goal.
Sergio Busquets was unfortunate not to add to Spain's tally when his long-range strike hit the crossbar, but Villa did finally add gloss to a hard-fought win when he slotted home Silva's cross following a clever turn late on.