Holders Spain fell behind early against Bosnia and Herzegovina but battled back to maintain their perfect start to UEFA European Under-21 Championship qualifying Group 4.
Visiting captain Srdjan Grahovac silenced the home crowd 13 minutes in when he gave the visitors a surprise lead. Sergi Roberto equalised just before the half-hour and further goals from Saúl Ñiguez and Álvaro Morata ensured Mario Kvesić's late effort was mere consolation.
Things were going according to plan early on as Óliver Torres and Iker Muniain both went close for Spain, but Grahovac tore up the script with a brilliant strike from 25 metres. Neither Morata nor Roberto could turn in the ball on 21 minutes when presented with a great double chance, but the hosts were soon level as the latter nodded in an equaliser after Rubén Pardo's free-kick had been saved.
Ñiguez powered in a corner with his head shortly after the break, before Morata applied a cool finish to Roberto's centre. Aldin Čajić's perfect pass left Kvesić with a simple job of pulling one back in the 86th minute, but it proved too late to prevent the three points going to Spain.