Gonzaga owned an 18-point lead at halftime and almost surrendered it all as St. Mary's came close to pulling off a major upset.
The Bulldogs improved their record to 16-1 and climbed to 3-0 in the West Coast Conference.
Matthew Dellavedova hit a 3-pointer that brought St. Mary's within three with 1:22 to play and his two free throws with 15 seconds remaining cut the Gaels' deficit to one.
Olynyk then hit his two foul shots, Gonzaga's first points in almost 3 minutes, to extend the Bulldogs' lead back to three. Dellavedova missed a 3-pointer with 8 seconds left that ended St. Mary's hopes
Olynyk made 11-of-19 shots from the field and scored 31 points. Kevin Pangos added 22 for Gonzaga, which shot 53 percent from the field.
Stephen Holt had 23 points for St. Mary's (12-4, 1-1).