Two Jacob Stockdale tries helped Ireland respond to their hammering by England seven days ago to beat a much changed Wales 22-17 in Cardiff, despite a late fight back from the home side.
Ireland led 22-3 with 20 minutes left, before two late Welsh tries gave the hosts hope of a remarkable comeback after a lacklustre performance. However the Irish held out for the final four minutes to inflict Wales' first loss in Cardiff since November 2017.
Wing Stockdale led the way with two first-half tries as Wales head coach Warren Gatland's final Principality Stadium Test before he departs after the World Cup ended frustratingly.
Ireland, crushed 57-15 by England last time out, bossed most critical areas ahead of next Saturday's return game in Dublin.
Fly-half Jack Carty added a conversion and penalty for the visitors, while his opposite number Jarrod Evans kicked a penalty before going off at half-time and being replaced by Rhys Patchell, with a second-half penalty try sealing Wales' fate.
Wales finished strongly, with debutant wing Owen Lane and Patchell claiming late tries and Patchell converting both, but it was not enough.
Gatland names his 31-man World Cup squad on Sunday, yet only Lane and Patchell made strong claims for any of the few remaining places.
Ireland boss Joe Schmidt, though, will have been encouraged by his team's recovery powers following a shambolic effort at Twickenham when England crushed them under an eight-try onslaught.
Stockdale, who departed at the interval, Carty, centre Bundee Aki, skipper Peter O'Mahony and his fellow flanker Tadhg Beirne were among those that impressed.