St Mirren led 2-0 - courtesy of goals from Paul McGowan and Steven Thompson - until 11 minutes from time, when Brian Graham ignored the appeals for offside to pull one back.
Fellow substitute Stuart Armstrong equalised before Nadir Ciftci's dramatic injury-time intervention sealed the points.
McNamara said: "I thought we were fortunate to win today.
"We got a break at our first goal. It looked offside although I would need to see it again but the lads showed great character to get it back to 2-2 and then pushed on for a winner.
"There was a lot of character from the team. I was kind of planning to make a few changes on the hour mark.
"Young Ryan Dow did really well when he came on when the game was a bit stretched and that helped.
"I thought St Mirren deserved a lot of credit for the way they played.
"They were excellent in the first-half. Take nothing away from them in that respect. I knew they wouldn't be able to sustain it for the full 90 minutes.
"It was a great response from us. I'm delighted about that and to get back to
winning ways. It's still a great three points for us under the circumstances."
Saints manager Danny Lennon felt his side got a raw deal at Graham's goal, adding: "It was a big decision which went against us.
"I've no doubts they had at least three men offside for their first goal. That gave them a helping hand at that stage in the game.
"We played really well up until then but that changed things completely. I'm really disappointed for the players for all they put into the match."