The home side were forced to dig deep.
Head coach Neil McRoberts said: ‘It was tough and we had to work hard to get on top.
‘Once we got a grip our structures and superior fitness showed.
‘We finished the game the stronger of the two sides.’
Denver Hume: Portsmouth's expensive forgotten man and why former Sunderland defender really could be one for the future
'Real driving force... lion-hearted display... classy presence' - Neil Allen's Portsmouth ratings from 3-0 Carabao Cup win at Cardiff
Cardiff 0 Portsmouth 3: Neil Allen's verdict - From Fratton boos to 'Just like watching Brazil' as brilliant Blues produce remarkable cup shock
Portsmouth transfer news: The four Fratton Park departures Blues could still sanction before window shuts - including ex-Sheffield United defender and Luton midfielder
'We’ve got a real problem': Portsmouth boss reveals headache after career first at Cardiff
Unfortunately home centre Gareth Richards suffered a leg injury during the game.
In an explosive start both teams scored converted tries in the opening eight minutes.
Trojans struck first but Portsmouth made a quick reply.
Mark Ovens fielded a clearance kick and linked with Luke Simmons to put Noah Cannon over wide out.
Cameron Duffus levelled matters with a superb conversion.
Duffus remained prominent converting his own try touched down near the posts.
Aided by two yellow cards suffered by their hosts the rest of the half belonged to Trojans.
With Portsmouth reduced to 13 players the visitors opened up a 21-14 lead at half-time.
Greg Hoolihan reduced the deficit with a try soon after the restart.
After Richards was forced to leave the field Simmons got in on the act with a magnificent individual effort.
He got to the line from inside his own half to score the bonus point try.
Duffus converted but the game remained on a knife-edge when a 73rd minute penalty brought Trojans back within two points at 26-24.
Portsmouth nerves were calmed when Cannon plucked a kick out of the air to score his second try.
The reliable Duffus again converted to give his side their bonus-point win.