Just three places separated the sides prior to the encounter, with the Foxes sitting two points above the Tykes in 13th.
Despite going into the game on the back of just one win in their last six games, Barnsley took the lead when Gray converted Daniel Drinkwater’s well-directed cross.
The hosts hit back impressively, though, and restored parity through Lloyd Dyer’s fine finish before Gray’s diving header put Keith Hill’s men back ahead.
Leicester, targeting back-to-back league victories for the first time this season, desperately tried to get back into the game.
However, they were restricted to half chances and, in the end, failed to claim a second win in their last seven league games.
Richie Wellens and Dyer returned to the Foxes’ starting XI, but the sides – both playing 4-3-3 -largely cancelled each other out in the opening exchanges.
It came as a surprise, therefore, when Gray opened the scoring in the 12th minute.
Drinkwater was instrumental, profiting from Ricardo Vaz Te’s fine work to send in a delicious centre, which Gray was on hand to prod home.
That goal seemed to spur Nigel Pearson’s side on and the scores were level three minutes later as Dyer raced on to Matt Mills’ wonderful ball over the top and produced a clinical finish.
Leicester began to dominate proceedings thereafter, with David Nugent looking particularly threatening.
But they were hit with a sucker-punch 11 minutes before half-time as Vaz Te and Drinkwater combined once more, the latter again sending in a fine cross, allowing Gray to head home.
Barnsley almost added to their lead just after the interval, but Kasper Schmeichel was on hand save at the feet of Vaz Te.
But the Foxes were enjoying the better of proceedings and came agonisingly close to levelling things up as Nugent headed against the crossbar.
The end-to-end nature of the game continued, though, with Vaz Te striking just over after Wellens’ effort hit the side netting.
Schmeichel again had to be on high alert moments later, saving substitute Matty Done’s effort.
Pearson brought on Jermaine Beckford as the hosts searched for an equaliser, but it was Dyer threatening once more as he hit just over after cleverly working an opening.
That was as much as Leicester could muster, though, with the visitors holding on comfortably to claim the points.