The result extended North End’s unbeaten league run to 15 games, but second-placed Preston are now just three points clear of MK Dons in third with four games to play.
“It feels like a defeat,” said Grayson. “But we’re still in a good position and there is still work to be done.
“We’ve got four games to go, we’re three points in front and we have to keep believing in what we’re trying to do.
“Given how well we played in the first half the game could have been over, and a couple of other games could have been over, with the number of chances we had. We passed the ball with real tempo, won our battles and looked a major threat.
“In the second half we didn’t get going, didn’t play with tempo. They won more individual battles. I don’t know whether the nerves kicked it, we’ve got to be braver. We needed to have a lot more composure and guile about us at times.
“The game should have been over but at 2-0 the next goal is always a major part of it.”
Jermaine Beckford gave Preston the lead after 36 minutes when he lashed the ball past Gillingham goalkeeper Glenn Morris from 16 yards.
Callum Robinson then doubled the advantage on the stroke of half-time but Jermaine McGlashan stunned Deepdale by pulling one back with the visitor’s first real chance after 58 minutes.
Preston, unbeaten at home in the league since November, almost hit back instantly but Joe Garner’s shot was blocked on the line by Cody McDonald.
And Legge made the hosts pay by nodding Max Ehmer’s cross over Sam Johnstone and into the far corner of the net in added time.
Gills boss Justin Edinburgh said: “We showed fantastic character to come back from falling two goals behind against a very good Preston side and it took all we had to get a point.
“It was hard conceding before half-time because, for as much possession as they had, there wasn’t too much to concern us.
“They punished us and to concede the second in first-half stoppage time was a big blow but credit to the players, they dug deep and found that resilience to get us back into the game.
“We needed to have a bit more belief in possession. I think we underestimate ourselves and how good we can be with the ball.
“My nature is to be competitive and win everything. I want to make sure we do things right. Standards are being set for next season as well as now. We’ve had to be very good in the second half to come back.”