Conor Newton shot the Premiership side ahead in the ninth minute but Iain Russell levelled for the home side midway through the first half.
Saints looked the better side after the break and appeared to be heading into the fifth round when former Rangers striker Steven Thompson (56) shot them in front before Michael Paton tapped home in the 72nd minute to end the scoring.
The visitors, who survived an early penalty scare when Marc McAusland fell on the ball in his own penalty area, took the lead when on-loan Newcastle midfielder Newton slotted home after Thompson flicked on a Kenny McLean corner.
Saints forward Paul McGowan limped out of the action 10 minutes later to be replaced by teenager Thomas Reilly in a passage of play which saw Queens finally take a hold of the game.
Visiting keeper Marian Kello did well to keep out a fierce Kevin Holt drive before Paton tested the Slovakian with a fine shot from just inside the area.
And their pressure finally told when Kello misjudged an Ian McShane free-kick allowing Russell to volley first time from 12 yards out.
Saints had two chances to restore their advantage - firstly Zander Clark saved well from a McAusland header before Jason Naismith somehow managed to screw the ball wide of the target with the goal at his mercy.
But they finally edged in front when Thompson exchanged passes with John McGinn, sub Reilly and Sean Kelly before slotting home in the 51st minute.
The goal failed to end the home side's resistance and they came close to levelling for a second time when Holt just failed to connect with a Paul Burns cross at the back post.
But that miss was soon forgotten when Burns picked out Paton with a perfect knockdown and he slotted beyond Kello to earn Queens a replay at St Mirren Park on December 10.