The normally dependable James Thornton stepped forward for the place-kick from the 22, and he made no mistake from a tight right hand angle.
This score was an early boost for the students and with their confidence levels improving with each passing minute, they crossed for the opening try after 10 minutes.
Some excellent approach play by centre Stephen Murphy paved the way for winger Sam Coghlan Murray to brilliantly ground in the corner, and with Thornton once again displaying unerring accuracy from the conversion that followed, UCD were showcasing the form that helped them secure the Leinster Senior League Cup trophy at Donnybrook last Friday.
St. Mary's, whose most recent league fixture was a 33-28 Friday Night Lights reversal against Lansdowne, were on the back foot at this stage but as the first half wore on, they started to exert their influence on the play.
An injury to scrum half Jamie Glynn 22 minutes in also upset UCD's rhythm and even though winger Steve Toal-Lennon did miss a kickable shot at goal, St. Mary's eventually opened their account in the 25th minute.
Toal-Lennon kicked to touch on this occasion and it proved to be a wise decision, as flanker Gareth Austen drove over to the left of the posts. Toal-Lennon added to his side's tally with a routine conversion as UCD's lead was cut to 10-7.
Austen's score provided a much-needed morale boost to the Templeogue outfit and although they failed to capitalise on some extended periods of pressure in the UCD half approaching the interval, they had certainly forced their way back into the reckoning.
Indeed, St. Mary's were able to bring their momentum from the first half into the second. A series of incisive moves forced UCD to leak a number of penalties.
Second row Darragh Frawley missed the first of these from long distance eight minutes after the restart, but Toal-Lennon split the posts with his kick from close in just moments later.
For the very first time in the game, St. Mary's were on level terms with their high-flying opponents. They were presented with an opportunity to move into the lead when Toal-Lennon stepped up to the kicking tee for the fourth time with 55 minutes gone.
The dynamic back could not find his range with this particular kick, but his third successful place-kick finally was slotted from a distance of 35 metres just past the hour mark.
UCD were struggling to stem the St. Mary's tide, however the 61st minute introduction of Niall Earls - in place of Thornton - looked like it was going to have major significance.
He split the posts superbly from distance in the 65th and 69th minutes respectively, and with the play becoming quite scrappy, there was a feeling that this would be enough to get the students over the line for their fifth league victory.
St. Mary's had other ideas though, and a superbly crafted five-pointer from Chris Lilly five minutes from time (following a cohesive move across the UCD defensive line) gave them a much-needed victory - just their second of the 2013/14 campaign.
Earls did have a couple of opportunities from long range in the dying minutes, but the UCD replacement out-half came up short much to the relief of the St. Mary's faithful.
Referee: Eddie Hogan O'Connell (IRFU)