The pre-tournament favourites dominated throughout but could find no answer to the organisation and work ethic of Ottmar Hitzfeld's side.
St Etienne midfielder Fernandes touched in from close range just after the interval to earn a win that confounded predictions.
Spain had won 28 of their previous 29 matches but slipped to one of the biggest upsets in the competition's history.
They controlled possession from the very start and David Villa's poor touch wasted a fine early chance to take the lead.
The Valencia winger David Silva felt he should have had a penalty when he went down in the area under a challenge from Philippe Senderos.
Silva and Andres Iniesta both had efforts from long range as Vicente Del Bosque's side continued their dominance.
Iniesta's prompting created a good opening for Gerard Pique, who cut inside his man and had a low stab at goal turned away by the Swiss goalkeeper Diego Benaglio.
Swiss defender Reto Ziegler's free kick was easily swatted away by Iker Casillas in the Spain goalkeeper's only work of the first half.
Recent Barcelona signing Villa had a free kick deflected wide and Iniesta also curled off target from just outside the area.
But Villa passed up another opportunity when he raced clear but crossed when he might have shot at goal.
Spain stepped up the tempo even further after the restart with Real Madrid's Xabi Alonso threatening from distance on two occasions.
But six minutes after the interval, the Swiss took a shock lead.
Spain failed to deal with a long clearance and after Casillas blocked Eren Derdiyok, Fernandes was on hand to touch in from close range.
Villa had a curling shot easily saved as stunned Spain attempted to get back on level terms.
The introduction of substitutes Fernando Torres and Jesus Navas brought Spain to life.
And Torres helped make space for Alonso to hit a drive that came back off the bar.
Switzerland continued to defend resolutely but Derdiyok could have added a second.
He skipped inside the challenges of Pique and Carles Puyol before his shot bounced away off the foot of the post.
But despite having to play out five minutes of injury time, Switzerland held on for a memorable victory.