Left-hander Gambhir hit 114 and Tendulkar, who became the first player to reach 30,000 international runs, made 100 not out to take the home team to 412 for four on the final day.
Tendulkar, who holds the record for most runs and centuries in test and one-day cricket, shared a 66-run fourth wicket stand with Gambhir and an undefeated 137 for the next with Vangipurappu Laxman (51 not out) as India avoided a first home test defeat by Sri Lanka.
The hosts wiped out a 334-run first innings deficit and were 78 ahead when play ended as soon as Tendulkar reached his century.
It was the seventh hundred of a high-scoring match.
Sri Lanka’s attack was weakened, with Dammika Prasad bowling just four overs on the final day due to a hamstring injury.
Resuming on 190 for two, nightwatchman Amit Mishra (24) dug in for 11 overs to deny the visitors an early breakthrough.
The 28-year-old Gambhir, who scored second-innings centuries in both tests in New Zealand in March and April, batted with poise to lead India’s fight to save the test.
Gambhir, 74 overnight, reached his seventh test ton by flicking medium-pacer Angelo Mathews to the fine-leg boundary.
Mishra fell to a splendid catch by Tillakaratne Dilshan at leg gully off Mathews but Sri Lanka’s bowlers found it difficult on an unresponsive pitch.
Gambhir, who collected 13 boundaries, fell after lunch when he mistimed a lofted drive off left-arm spinner Rangana Herath and was caught by Prasad at mid-off.
Tendulkar was particularly severe on Muttiah Muralitharan as he cracked the spinner for a series of boundaries. The 36-year-old former India captain struck 11 fours in all.
Muralitharan, who holds the record for most wickets in test and one-day cricket, is playing in his last overseas test tour.
Sri Lanka had declared their first innings on 760 for seven on day four.
Man of the match Mahela Jayawardene scored his sixth double century in tests, with Prasanna Jayawardene and Dilshan adding tons.
Earlier, centuries by Rahul Dravid and skipper Mahendra Singh Dhoni had led India to 426 in their first innings.
The second game in the three-test series is in Kanpur starting on Tuesday.
SCOREBOARD
India first innings 426 (R. Dravid 177, M. Dhoni 110)
Sri Lanka first innings 760-7 dec (T. Dilshan 112, M. Jayawardene 275, T. Samaraweera 70, P. Jayawardene 154 not out)
India second innings (190-2 overnight)
G. Gambhir c Prasad b Herath 114
V. Sehwag c Mathews b Herath 51
R. Dravid lbw b Welegedara 38
A. Mishra c Dilshan b Mathews 24
S. Tendulkar not out 100
V. Laxman not out 51
Extras (b-12, lb-9, w-2, nb-11) 34
Total (four wickets; 129 overs) 412
Fall of wickets: 1-81 2-169 3-209 4-275
Did not bat: Y. Singh, M. Dhoni, H. Singh, Z. Khan, I. Sharma.
Bowling: Welegedara 21-1-76-1 (2nb, 2w), Prasad 13-0-56-0 (3nb), Herath 40-6-97-2, Muralitharan 38-6-124-0 (6nb), Mathews 15-6-29-1, Dilshan 1-0-2-0, Paranavitana 1-0-7-0.
Second test: Nov 24-28, Kanpur
Final test: Dec 2-6, Mumbai