Denis Zakaria's own goal gave Spain an eighth-minute lead but Xherdan Shaqiri equalised for Switzerland on 68 minutes after a defensive mix-up, before Remo Freuler was sent off.
Spain goalkeeper Unai Simon saved from Fabian Schaer and Manuel Akanji in the shootout before Mikel Oyarzabal converted the winning kick after Ruben Vargas blazed over.