The Boro defender picked himself up off the floor having been brought down by Nathan Thompson to score the decisive opener with five minutes remaining.
And in stoppage time 18-year-old Josh Coburn stroked in the second to frustrate Peterborough, who have now lost all six of their away games since returning to the Sky Bet Championship.
It had looked like Peterborough, sitting in the relegation zone, would pick up a first point on the road in a game that had been starved of clear-cut chances.
Middlesbrough, who are now five points shy of the top six after winning for the third time in 10 games, had struggled to seriously test Peterborough goalkeeper David Cornell.
Neither goalkeeper had to make a save until Cornell got down low to comfortably gather Marcus Tavernier’s effort in the 42nd minute.
Before that there were a few scares for the defenders but very little to really threaten.
Andraz Sporar headed wide from Onel Hernandez’s cross early on and Duncan Watmore’s run from his own half ended with Cornell holding the low cross quarter of an hour later.
The nearest Peterborough, who started brightly with a couple of probing attacks, came to scoring was when Jorge Grant headed over Sammie Szmodics’ centre six minutes before the break.
Hernandez had also seen a late effort cleared off the line by Josh Knight when he was played in behind his marker and he had beaten the keeper with his shot.
The biggest positive for Middlesbrough at that point was they had kept a clean sheet, having entered into this game without their first-choice back four. Anfernee Dijksteel, Marc Bola, Dael Fry and Grant Hall are all injured and Lee Peltier was also suspended.
Middlesbrough were quick to create a chance after the restart too. Uche Ikpeazu’s delivery was headed wide by Slovenian Sporar.
Almost immediately Peterborough had shouts for a penalty waved away at the other end when goalkeeper Joe Lumley missed the ball and appeared to catch Siriki Dembele.
After that, Middlesbrough created a couple of openings with balls whipped across the six-yard box, but there was frustration as a red shirt could not get on the end of it.
Hernandez also created an opening when his low drive had to be held by Cornell, but once again there was no way through.
But in the closing stages McNair was pulled to the ground by Thompson and referee John Busby pointed to the spot. McNair got up and sent Cornell the wrong way.
And then teenage substitute Coburn sealed the points with a tidy finish in stoppage time after exchanging passes with Martin Payero.