Matty Kennedy gave Saints the lead just before half-time but Alfredo Morelos headed the equaliser and the Colombian striker grabbed a late winner with a fine finish past Zander Clark.
The victory moves Steven Gerrard's men to within a point of leaders Celtic at the halfway stage although the reigning champions have a game in hand.