Iraq's Aymen Hussein turned from hero to zero as, moments after putting Iraq ahead, he was sent off, having received a second yellow card for excessive celebrations at Khalifa International Stadium.
Down to 10 men, Iraq could not withstand Jordan's fightback and conceded twice in stoppage time to end their hopes of winning the competition for a second time.
Hussein struck in the 76th minute to spark wild celebrations, including him sitting cross-legged on the field and mimicking eating.
Iraq's joy soon turned to frustration, though, when referee Alireza Faghani booked Hussein for a second time.
It got worse for the 2007 champions when Yazan Al Arab levelled in the fifth minute of added time, with Nizar Al Rashdan going on to drill in a winner two minutes later.
Jordan advanced as one of the best third-placed teams and took the lead in the first minute of first-half stoppage time through Yazan Al Naimat.
Suad Natiq equalized for Iraq prior to Hussein's fifth goal of the tournament sparking the chaotic finale.
Jordan will play tournament debutants Tajikistan in the last eight.
In the day's other match, Akram Afif delivered again for Qatar as the hosts and defending champions overcame Palestine 2-1.
Afif has established himself as one of the stars of the tournament, and if he can keep up this form he may yet achieve his ambition of joining a European club.
The forward struck from the penalty spot in the 49th — his fourth goal of the Asian Cup — as Qatar came from behind at Al Bayt Stadium, with Hassan Al Haydos having cancelled out Oday Dabbagh's clinical finish.
Qatar will play either Uzbekistan or Thailand for a place in the last four.
Luis Enrique's men were dominant throughout and took the lead through Fati's blasted effort early in the first half.
Gavi finished beautifully for their second before Williams added a third, and though Jordan scored a late consolation, Spain made it eight wins from their last 11 matches as they head to Qatar in excellent form.
Spain will now turn their attentions to putting together the finishing touches ahead of their World Cup campaign, having not made it past the round of 16 since winning the tournament in 2010.
Fati put Spain ahead after 13 minutes when Marco Asensio laid the ball off to him in the box before the Barcelona man's low shot went in under Jordan goalkeeper Abdallah Al Fakhouri.
Jordan had a great chance to level moments later, though Yazan Al Naimat could only fire straight at Robert Sanchez after catching Pau Torres in possession just outside Spain's box.
Fati nearly scored his second before the break, but his looping effort landed on top of the net as he continued to threaten down Spain's left.
Gavi made it 2-0 after the interval, the ball falling kindly for him in the box before the midfielder coolly stroked into the bottom right corner.
Williams scored a third with a sublime late strike, shifting onto his left foot before slamming into the far corner after receiving a pass from Yeremy Pino.
Jordan pulled a goal back in injury-time, Hamza Al Dardoor bringing a Mohammed Abu Zurayq cross from the right down before poking past substitute goalkeeper David Raya, though it was not enough to stop Spain from heading to the World Cup in high spirits.
The Reggae Boyz last faced Qatar on August 26th last year, playing out a 1-1 draw while this will be the first time Jamaica and Jordan will be doing battle.
JFF President Michael Ricketts said the matches are to give the Reggae Boyz the best opportunities to prepare for the Gold Cup.
“We want to place coach Hallgrimsson and his team in the best position to perform at their optimum at the Gold Cup,” Ricketts said.
“This is the third year consecutively that we will be hosted in this city, a location that is perfect for preparation. I believe that with the quality of the team we now have, combined with the quality of coaching, we have an outstanding opportunity to create history by winning the coveted trophy.”
Jamaica’s first match of the 2023 Gold Cup will come against defending champions USA on June 24th at Soldier Field in Chicago.