Ronan Curtis fired them ahead in the early stages and then restored the lead with his 50th goal for the club after Ryan Jackson had levelled for the visitors.
The Blues deserved more for their dominance, though, and Clark Robertson headed home following the interval to wrap things up.
Danny Cowley made two changes from the side that were cruelly denied victory at Morecambe on Monday.
Joe Morrell and Louis Thompson both returned to the engine room, with Ryan Tunncliffe and Shaun Williams dropping to the bench.
Meanwhile, former Pompey loanees Ben Thompson and Stuart O’Keefe were selected in the visitors’ midfield.
Before the game there was the award of The News/Sports Mail Player of the Season trophy, with readers of our local papers selecting Sean Raggett for the honour.
A minute’s applause was then held to remember all members of the Pompey family who have passed away during the course of the campaign.
The first half was a curious affair, with plenty of excitement at the start and finish and not a lot happening in between.
It was in the fourth minute when Marcus Harness burst down the right and played a neat one-two with Hayden Carter.
He then sent a low cross into the box, where CURTIS was waiting to slam the ball home off the underside of the bar.
The hosts continued to dominate both possession and territory, but were unable to make the most of that.
And they were made to pay on the half-hour mark, as Jackson lost Curtis at the back post to angle home from a Ben Reeves delivery.
There were a couple of blocked shots from the Blues, but nothing to suggest they would retake the lead.
That changed in the second minute of stoppage-time, when Jackson was unable to deal with a Raggett cross.
The ball reached CURTIS at the back post and he showed plenty of composure to arrow it into the bottom corner.
Gillingham were furious, seemingly feeling that referee Darren Drysdale should had already blown for the interval.
The protests of boss Neil Harris saw him sent to the stands, while another member of the coaching team was booked.
The second half took some time to get going, but the Fratton faithful soon had another goal to celebrate.
Jacobs sent a corner into the box and ROBERTSON rose above David Tutonda to plant a bullet header into the net.
They might have gone further ahead a few minutes later, with Michael Jacobs only able to find the side netting from an acute angle.
And Curtis tried to complete a hat-trick, but fired into Aaron Chapman’s midriff after collecting a Carter cross.
Pompey were coasting and passing the ball around nicely, prepared to stay patient in search of an opening.
Cowley gave Jay Mingi an opportunity to gain some minutes on the pitch, with Louis Thompson the man to make way.
And former Gills defender Connor Ogilvie then limped off with an injury to be replaced by Mahlon Romeo.
Aiden O’Brien also came off the bench in the closing stages and had a stoppage-time shot turned around the post by Chapman.
But the game had been drifting towards a conclusion before then and Pompey saw out the final moments to secure a deserved win.
Pompey (4-2-3-1): Bazunu; Carter, Raggett, Robertson (c), Ogilvie (Romeo 78); Morrell, Thompson (Mingi 74); Harness (O’Brien 89), Jacobs, Curtis; Hirst
Goals: Curtis 4, 45+2, Robertson 54
Subs not used: Webber, Williams, Tunnicliffe, Walker
Gillingham (3-5-2): Chapman; Tucker, Ehmer, Masterson; Jackson, Thompson, Lee, O’Keefe (c) (Phillips 46), Tutonda (Dickson-Peters 64); Oliver, Reeves
Goals: Jackson 30
Booked: Tutonda, Ehmer
Subs not used: Dahlberg, Lintott, Maghoma, Chambers, Sithole
Referee: Darren Drysdale
Attendance: 15,540 (1,192 away fans)