A looping 90th minute header from substitute Marta Cardona settled what was an absorbing clash at the Brentford Community Stadium.
And it sets the scene for a last-eight knockout clash with the tournament hosts next Wednesday.
With the Spaniards needing just a point to progress but only a win being sufficient for Denmark, it made for a compelling Group B finale in front of a crowd of 16,041 in south west London.
While the Danes looked to pounce on the counter-attack and utilise the cutting edge provided by star striker Pernille Harder, Spain’s metronomic passing game saw them carve open a host of first half opportunities.
Athenea del Castillo could only direct a close range 26th minute effort straight at Danish keeper Lene Christensen who then earned her spurs again with another good block to deny Mariona Caldentey’s 32nd minute effort.
And just before the half time whistle blew, the Spaniards were frustrated once again as Irene Paredes' goalbound header from a corner was cleared off the line by Simone Boye.
Ouahabi was one of three Spanish players substituted at the break as Jorge Vilda’s sought to engineer the breakthrough.
One of the new arrivals, Olga Carmona almost made a quick impact as she saw a smart 52nd minute shot hammer into the side netting.
But with the match still on a knife edge, Danish substitute Nadia Nadim came close to breaking the deadlock after a lightning break, with the former City striker’s 76th minute effort requiring a smart save from Spain keeper Sandra Panos.
Aleixandri was then introduced and the City defender was straight into the action seeing her 82nd minute header deflected for a corner.
But with time almost up, La Rioja finally got the goal their efforts deserved. Another patient build-up saw Cardona latch onto an inviting Carmona cross before cushioning a 12 yard header into the far corner of the net to clinch victory.
It sets up a mouth-watering clash with Sarina Wiegman’s England at Brighton’s Amex Stadium next Wednesday, kick-off 8pm, for what should be the match of tournament so far.
Watch City’s European stars in action at the Academy Stadium next season. Find out more.