David Silva and Diego Costa were on target but Spain made hard work of seeing off Macedonia in Skopje.
The visitors were 2-0 up and cruising but left the back door open midway through the second half, allowing Stefan Ristovski to pull one back.
Nevertheless, a 2-1 victory ensured Spain remain top of Group G on goal difference despite Italy's 5-0 win over Liechtenstein.
Spain took the lead after 15 minutes following a classic Spanish build-up.
Andres Iniesta casually scooped the ball over the Macedonia...