Sweden took the lead early in the second half through Marcus Berg’s header from close range.
David de Gea made a stunning save from Robin Quaison’s header, before hobbling off injured in the second half.
The goalkeeper is a doubt for Manchester United’s Premier League game against Liverpool on Sunday with what appeared to be a hamstring injury.
Spain should have had more but Sweden goalkeeper Robin Olsen made sharp saves to deny Manchester City’s Rodri, Ruiz and Gerard Moreno.
They become the sixth team to qualify for next year’s tournament after Belgium, Italy, Russia, Poland and Ukraine.