The midfielder's left-sided free-kick flew right through the crowded penalty area and into the top corner, prompting jubilant celebrations from Scotland's players.
However, following a VAR check, it was ruled out.
The hosts added insult to injury just minutes later, when Alvaro Morata's glancing diving header beat Angus Gunn to give Spain the lead.
Oihan Sancet then added a second in the dying moments to put the result beyond Scotland, making them wait to confirm their qualification for Euro 2024.