The Six Nations champions overhauled a nine-point deficit as Ross Moriarty scored the winning try just a few minutes from the end.
Wales are through to the semi-finals of the Rugby World Cup after narrowly beating France 20-19.
The Welsh will now face either Japan or South Africa for a place in the final - those teams are playing later in Tokyo on Sunday.
Ross Moriarty scored the winning try which was converted by Dan Biggar as Wales narrowly took the lead in the 74th minute of the match in Oita.
The Six Nations champions managed to overhaul a nine-point deficit after French lock Sebastien Vahaamahina was shown a red card for foul play - elbowing Wales flanker Aaron Wainwright in the face - in the 49th minute.
Wainwright had earlier scored Wales' first try. The boot of Biggar got the side's other points.