Iran must now await the outcome of the final match tonight between Senegal and Brazil, and they will qualify provided Senegal do not collect any points.
Olexandr Kornichuk put the Europeans in front at the midway point of the middle period with a superb strike.
Iran looked more dangerous in the third period but Ukraine goalkeeper Vitalii Sydorenko remained a challenging obstacle. Captain Igor Borsuk then put his side ahead with another excellent strike for the Ukrainians.
Goalkeeper Peymann Hosseini reduced the deficit late on, leaving Ukraine chasing more goals to have any hope of progressing.
Ukraine put a field player on goal in the last minute, but that was not enough to avoid elimination despite the victory.