Wales moved off the bottom of Group Six after claiming their first win of the World Cup qualifying campaign.
Simon Davies fired home Sam Ricketts' cross to put Wales ahead and Ryan Giggs set up Carl Robinson for their second.
John Hartson should have made it three but his weak penalty was saved by Maik Taylor, and the hosts fought back.
Keith Gillespie's neat finish was followed by a powerful equaliser from Steve Davis but a curling free-kick by Giggs gave Wales a dramatic victory.
Northern Ireland nearly got off to the perfect start on two minutes when Michael Duff's close-range header from a Gillespie corner clipped the crossbar.
The home side made the better start at a bright and breezy Windsor Park but Wales soon showed their threat in attack.
Robert Earnshaw sent his 20-yard strike rising over the bar in the eighth minute and a long-range effort from Davies went well wide three minutes later.
Colin Murdock wasted a second headed chance for N Ireland after 13 minutes, missing the target from a Davis centre.
The visitors broke the deadlock on 27 minutes with Davies connecting with Ricketts' cross to fire low and hard into the bottom corner.
Sunderland midfielder Robinson doubled the lead 11 minutes later with a clinical close-range volley after a superb run and cross from Giggs.
Wales were awarded a penalty on 40 minutes when Taylor's poor kick put Tony Capaldi in trouble and the full-back pulled back Davies as he ran into the box.
Capaldi was booked for the offence and is suspended for Wednesday's qualifier with Austria.
Celtic striker Hartson stepped up but his weakly hit spot-kick was saved by the sprawling Taylor.
Northern Ireland winger Stuart Elliott found the net two minutes later but it was ruled out for offside.
The half ended on a sour note for Wales when Mark Delaney was booked for time-wasting - the Aston Villa defender will now miss the final qualifier against Azerbaijan on Wednesday.
Northern Ireland hit back straight after the break when Gillespie slotted the ball into the bottom corner after a clever one-two with David Healy.
Elliott was booked for a poor challenge on James Collins before Northern Ireland drew level in the 49th minute.
A long throw from Duff was chested down by James Quinn and Davis hit a powerful volley just inside the post.
The injured Collins could not continue and he was replaced by Coventry defender Richard Duffy after 52 minutes.
The impressive Giggs restored the lead for Wales on 61 minutes with a curling 20-yard free-kick which beat the wrong-footed Taylor.
A blistering shot from Davies was pushed out by Taylor before Northern Ireland twice went close to an equaliser.
Johnson was clear on goal after a poor 67th-minute clearance but hit his shot straight at keeper Paul Jones and two minutes later, Jones turned Healy's effort round the post.
Northern Ireland were applying the pressure and the Welsh were forced into some last-ditch defending.
Dave Partridge became the second Welsh defender to be banned for the final qualifier after being booked for a foul on Healy on 81 minutes.
Healy's goalbound shot was blocked by Partridge in injury time as Wales held on for their first win in Group Six.
Cheltenham midfielder Grant McCann has been drafted into the Northern Ireland squad for the Austria game on Wednesday.
Northern Ireland: Taylor, Duff, Murdock, Craigan, Capaldi, Gillespie, Johnson, Davis, Elliott, Quinn, Healy. Subs: Ingham, Sproule, Smith, McAuley, Jones, Brunt, Feeney.
Goals: Gillespie 46, Davis 49
Wales: Jones, Delaney, James Collins (Duffy 52), Partridge, Ricketts, Davies, Fletcher, Robinson, Giggs, Earnshaw, Hartson. Subs: Brown, Duffy, Vaughan, Williams, Cotterill, Crofts, Danny Collins.
Goals: Davies 27, Robinson 38, Giggs 61
Referee: Ruud Bossen (Holland)