Emergency services were called to Balfour’s £28m Portobello High School job in Edinburgh last week.
The guard went to investigate after hearing what he thought were intruders and got stuck in the mud on his patrol.
The guard tried desperately to free his leg but had to call his boss when all efforts failed.
The off-site security supervisor then alerted the emergency services, who launched a full-scale rescue attempt amid fears the man’s life could be in danger.
A spokesman for the fire service told the Edinburgh Evening News: “At 9.43pm we received a call from the construction site security control stating that there was a man with his leg trapped in the mud.
“Six appliances were sent because of the nature of the incident. We thought it was a possible water rescue.”
Once it became clear the man’s plight was not life-threatening, specialist teams were stood down and the victim was dug out with a shovel.
The man, who was suffering from the effects of cold and rain, was stranded in the mud for around 45 minutes.
Temperatures had been close to freezing and he was checked for hypothermia by ambulances at the scene.
A spokesman for Balfour Beatty said: “While conducting a routine patrol on the site of the new Portobello High School on Thursday night, a security guard became stuck in a stockpile of soft mud, as he was investigating a noise which caused him to leave his designated route.
“On becoming stuck, he called his off-site security supervisor who called the emergency services and came on site to assist.
“On arrival, the emergency services removed him from the mud. While a full investigation is still ongoing, we can confirm that the security guard was checked by emergency services and suffered no injuries.
“We are currently reviewing the processes around the protocols security guards should follow on leaving the agreed routes for patrols.”