The result put Panama in third place in Group B with three points, one behind Spain and Iran. The Central Americans will play their last group match on Thursday against Iran.
Miguel Lasso and Fernando Mena both hit the post within the first minute, but Kike volleyed a pass from the edge of the area in the second minute and Lin added another goal six minutes later to give Spain control.
Borja scored the first of two goals in the 11th and Aicardo had the first of his two three minutes later to stake Spain to a 4-0 advantage.
Panama finally replied in the 17th when Michael de Leon took goalkeeper Jaime Londono's long throw, let it bounce once and volleyed the ball from two meters outside the penalty area past Spanish keeper Cristian.
Outshooting Panama 50-34 - including 26-10 on goal, Spain increased its lead one second before halftime with another goal by Aicardo.
De Leon had a second in 33rd and Alquis Alvarado also scored for Panama, but that was more than matched by Borja's second in the 22nd, a penalty by Alemao in the 27th and Miguelin's tally in the 30th.