Wales claimed a first win of the World Under-20s Championship but made hard work of it against 14-man Japan.
Japan led 19-15 when captain Yoshiki Omachi was sent off early in the second half for a dangerous tackle.
Wales still trailed with 15 minutes remaining in heavy conditions before a late flurry, including three tries in the final five minutes for Dan Edwards, Bryn Bradley and Louie Hennessey.
Mark Jones' side are second in Pool A after France crushed New Zealand.
Jones had made just two changes to the team that was edged out by New Zealand 27-26 in the opening game in South Africa.
Louis Fletcher replaced tight-head prop Ellis Fackrell who has been ruled out of the tournament with a shoulder injury.
Ospreys lock Evan Hill made his first start and would end up on both sides of cards.
Edwards' early penalty was the only points of the opening exchanges as both sides struggled on the wet surface at Danie Craven Stadium in Stellenbosch.
But when scrum-half Hughes finished a scything break by Harri Houston and Bryn Bradley, Wales looked comfortable.
However, the game turned when Hill was shown a yellow card for a dangerous tackle on Japan hooker Renjo Oike.
Japan made the most of their extra man with two identical tries from driving line-outs, both finished by Oike.
Hill returned but Japan, showing eight changes from the 75-12 hammering to France, were buoyant and wing Kota Nagashima stretched out of Cameron Winnett's tackle for a third try in 15 minutes.
Wales, trailing 19-10, needed a response and it came through Lloyd when the hooker peeled off the back of a line-out drive, stepped out of a tackle and scampered over on the stroke of half-time.
The game looked to have swung Wales' way early in the second half when Omachi needlessly charged into Hill at a ruck.
Initially the captain and centre was shown a yellow card, but the sanction was upgraded on review by the television match official as part of a pilot scheme at this championship.
However, with a struggling set-piece, it was not until 65 minutes that Wales' fitness made the extra player count as Japan gifted opportunities.
Replacement Tom Florence squeezed over in the corner from Hughes' long pass for a slender 20-19 lead before Edwards then sealed a bonus point five minutes from time.
Outstanding number eight Morgan Morse twice broke away in the final moments to set up tries for Bradley and Hennessey for a scoreline than barely reflected Wales' struggles.
Wales' final Pool A match on Tuesday is against defending champions France, who beat New Zealand 30-12.
Wales interim head coach Mark Jones told S4C:
"Japan really fronted up, especially at the set-piece where they probably edged us out for large parts.
"We always knew it was going to take an 80-minute performance and the dam may burst eventually under constant pressure. I just wish it had burst a little earlier.
"We got what we wanted from this game in terms of points but our discipline was poor and we know we've got to go up another three or four gears when we face France."
Wales U20s: Cameron Winnett (Cardiff); Llien Morgan (Ospreys), Louie Hennessey (Bath), Bryn Bradley (Harlequins), Harri Houston (Ospreys); Dan Edwards (Ospreys), Archie Hughes (Scarlets); Dylan Kelleher-Griffiths (Dragons), Lewis Lloyd (Ospreys), Louis Fletcher (Ospreys), Evan Hill (Ospreys), Jonny Green (Harlequins), Ryan Woodman (Dragons - capt), Lucas De La Rua (Cardiff), Morgan Morse (Ospreys).
Replacements: Sam Scarfe (Dragons), Josh Morse (Scarlets), Kian Hire (Ospreys), Mackenzie Martin (Cardiff), Seb Driscoll (Harlequins), Tom Florence (Ospreys), Harri Wilde (Cardiff), Harri Williams (Scarlets).
Japan U20s: K Muto; R Oike, S Taira, Y Omachi (capt), Y Yazaki; K Naramoto, Y Takahashi; H Sena, K Nagashima, R Tomita, Y Sasaki, H Willard, K Miyashita, T Kusuda, T Mogami.
Replacements: T Omoto, T Yumbe, K Sugiura, B Kurita, T Kobayashi, T Ohshima, G Ikuta, R R Imano.
Referee: Morne Ferreira (South Africa)