Gillingham’s defeat meant Rovers escaped the drop in front of more than 3,000 travelling fans.
They took a well-deserved lead in the 38th minute when Ian Goodison crashed a close-range shot into the roof of the Stockport net after playing a neat one-two with Bas Savage.
Shortly before the break Joss Labadie almost made it two when his 30-yard drive was superbly beaten away by Stockport keeper Lloyd Rigby.
It was 2-0 in the 53rd minute when Zoumana Bakayogo crossed from the left and former Stockport striker Ian Thomas-Moore volleyed in from eight yards.
Tranmere wrapped up the win in the 76th minute when Labadie tapped home after Rigby spilled a shot from Savage.