Needing only a draw to qualify for the last four, Thailand were the better side for large parts of the match and made their superiority count when Arthit Sunthornphit struck seven minutes after half-time direct from a free-kick. However, nine minutes from time Nasriq Baharom scored at the far post after a fine cross from the left by Azmi Muslim to level the scores and give Malaysia hope.
Then, on 90 minutes, Ahmad Fakri Saarani won the match for Malaysia, firing home a cross from the right by Norshahrul Idlan Talaha to make it 2-1 and spark wild scenes of celebration.
Elsewhere, Vietnam topped Group A with 10 points after thumping Cambodia 6-1, with Malaysia on nine points and Thailand, who had won every SEA Games football tournament since 1993, on seven. East Timor finished bottom of the five-team group with no points after four straight defeats. Host nation Laos and Singapore qualified for the semi-finals of the U-23 competition on Thursday from Group B.