The game was tied at 2-2 in the closing minutes, and Algeria looked as if they were content to run out the clock, when captain Riyad Mahrez scored his second goal with about a minute left in stoppage time.
That put Austria on the verge of elimination, only for Sasa Kalajdzic to head in the equalising goal moments later and rescue Das Team's World Cup hopes.
Marko Arnautovic and Marcel Sabitzer also had goals for Austria, who finished second behind Argentina in Group J to advance for the first time since 1982.
Their reward is a matchup with European champion Spain on Thursday (Friday AEST) in Los Angeles.
Rafik Belghali also scored for Les Fennecs, who became the ninth of 10 teams from Africa to advance. They finished third in the group but get a potentially easier Round of 32 showdown with Switzerland on Thursday night in Vancouver, British Columbia.
Iran would have advanced as one of the eight best third-place teams had Austria or Algeria won.
Austria struck first on Saturday night (Sunday AEST) when Arnautovic perfectly timed his run between the two Algerian centre backs, found himself one-on-one with goalkeeper Oussama Benbot, and overcame a stumble to score his record-extending 49th career goal for his nation.
Algeria answered just before halftime, when Belghali's left-footed shot easily beat Austria goalkeeper Alexander Schlager.
The frenetic pace continued in the second half on a hot night in Kansas City.
Not content with a 1-1 draw, Austria's Konrad Laimer sent a sharp pass across the field that Sabitzer finished to regain the lead — and give Iran some hope — only for Algeria to answer minutes later, when Mahrez scored off a perfect cross from Houssem Aouar.
Algeria took control down the stretch, playing keep-away as the crowd grew antsy. But just when it seemed both teams were happy to run out the clock, Mahrez and Kalajdzic provided them with some lasting fireworks in stoppage time.