Acting skipper Rory Best grabbed one of his side's tries as the home team led 22-14 at the break before adding a fourth try near the end of a closely fought contest.
Ulster hooker Best was leading Ireland for the first time in the championship as he stepped in for the injured Paul O'Connell. And his was the first try of the four, the others coming from Eoin Reddan, Andrew Trimble and Fergus McFadden. Jonathan Sexton added 12 points with his boot.
Gray's spectacular gallop, which ended with a huge dummy to bypass Rob Kearney before dotting down for the try, was the highlight of a competitive first-half performance from the Scots. Greg Laidlaw landed three penalties and the game was finely balanced at the break.
But the Scots failed to add to their tally in the second half and a later try from McFadden, allied to a penalty and conversion from Sexton, saw the Irish home to their second successive home win.