Set 287 for victory, Gambhir made a magnificent 102 and although he was run out with the tourists still requiring 91 runs, Raina's unbeaten 65 coupled with Pathan's 34 not out saw India home as they took a 2-1 lead in the five-match series.
Earlier, Angelo Mathews' late burst helped Sri Lanka to a formidable 286 for five after they had won the toss.
Mathews struck an unbeaten 71 off 57 balls to build on the foundations laid by Kumar Sangakkara (73) and captain Mahela Jayawardene (67) after Sri Lanka had collapsed to 20 for three.
The two veteran batsmen put on a stand of 121 for the fourth wicket while Mathews and Jeevan Mendis (45 not out) added an unbroken 104 for the sixth wicket.
While Sangakkara and Jayawardene steadily accumulated, between them hitting just 10 fours, Mathews and Mendis put India's bowling to the sword, with the former striking five fours and a six in an impressive innings.
In response, Gambhir was the anchor of India's innings, which included a stand of 105 for the second wicket with Virat Kohli (38) although Sri Lanka paceman Lasith Malinga then took the wickets of Mahendra Singh Dhoni and Rohit Sharma in successive balls.
Gambhir reached his century off 96 balls but was then run out after attempting a quick single shortly after. With the game in the balance at 196 for five, Raina and Pathan put on an unbroken 92 for the sixth wicket to steer the World Cup winners to victory with just two balls to spare.
Sangakkara will miss the rest of the ODI series after fracturing a finger on his right hand. The classy left-hander, on 24 at the time, was struck on the glove in the 15th over off Ashok Dinda but decided to carry on batting in spite of the blow.
He faces four to six weeks on the sidelines and may now be a doubt for the World Twenty20, which gets under way in Sri Lanka on September 18.