The Swiss showed immense character to push the former European champions the distance despite going a man down in the second half.
Alvaro Morata scored his fifth goal at the European Championships, becoming Spain's joint-highest scorer in the competition along with Fernando Torres.
Spain finally clicked into gear after two draws, but will face its toughest test yet in the round of 16 from Croatia.
Spain play Croatia in the last 16 on Monday; In the other match, Viktor Claesson sends Sweden top, while Poland are eliminated despite Robert Lewandowski’s brace.
The final round of Group E matches in the Euro 2020 group stages sees Sweden take on Poland while Spain will face Slovakia.
The centre-back, who is currently on Euro 2020 duty with Spain, played a starring role for Villarreal last season, helping them win the Europa League title.
Needing to win, Spain were heading out as they entered added time 3-2 down; then came an incredible comeback.
With his time at the club now over, Sergio Ramos leaves behind a legacy that, in the future, will be a benchmark for Real Madrid players to emulate.
Head coach Luis Enrique’s decision to start Alvaro Morata over in-form Gerard Moreno proved costly as La Roja frustrated in a goalless draw against Sweden.
With three years having passed since the day, let us take a look back at a day when Cristiano Ronaldo rewrote history books.