The visitors could have taken the lead as early as the fifth minute but Craig Reid saw an effort go wide of the target after confusion inside the penalty area.
Up the other end, Mark Stewart glanced a header wide for the home side, before St Mirren goalkeeper Jamie Langfield came to the rescue three times in quick succession, which included stopping a close-range header from Iain Davidson.
However, six minutes before the break, Raith finally went in front through Stewart, who fired into the top corner from inside the six-yard box.
Just before the break, Calum Gallagher almost found an equaliser, only to see Kevin Cuthbert make the save, but it appeared as though St Mirren would end the night empty-handed after their hosts controlled the second half.
Raith could not hold on to their lead, though, as two minutes from time, Mallan stepped up to fire a set piece into the top corner to move his side into seventh place in the Scottish Championship table.