Gayle became the first West Indian to score a triple century on foreign soil and the fourth player to score two triple tons, joining Australian great Don Bradman, India's [ Images ] Virender Sehwag [ Images ] and compatriot Brian Lara [ Images ].
With the batsmen swiping away the Sri Lankan attack with ease on the placid pitch, West Indies looked set for a huge first innings total but the dismissal of Brendan Nash [ Images ] (64) sparked a collapse and the hosts hit back to claim six wickets for 21 runs.
Sri Lanka will resume the third day on 54 for one.
Jamaican Gayle caressed Ajantha Mendis [ Images ] to the extra cover boundary to bring up his triple century, and promptly dropped down on his knees in celebration while his team mates applauded him loudly from their balcony.
Just when it seemed that Gayle could surpass Lara's record mark of 400, spinner Mendis brought Sri Lanka back into the match with a great spell of bowling in the final session and claimed five wickets for 13 runs off 46 balls.
He broke a 167-run fourth wicket stand between Gayle and Nash by trapping the latter lbw. He went on to pick up the scalps of Dwayne Bravo [ Images ] (five), Gayle, Darren Sammy (naught) and Andre Russell (two) to take his second six wicket haul at this venue finishing with figures of six for 169.
Gayle looked in control until he suffered cramps on his right leg after passing his previous highest score of 313 and had to be assisted by a runner. He struggled to handle the spinners and eventually was bowled between bat and pad by Mendis when he failed to get to the pitch of the ball.
Gayle returned to a standing ovation from the pavilion having scored his runs off 437 balls with 34 fours and nine sixes.
The hosts bowled a much tighter line to slow down the scoring rate as the West Indies lost Shivnarine Chanderpaul [ Images ] for 32 before going to lunch on a formidable 429-3.
Sri Lanka believed they had trapped Gayle leg before when they used up their second referral of the innings off the first ball of the day when Mendis rapped the fully stretched opener on the pads.
However, the decision went in favour of the batsman meaning Sri Lanka had lost out on both referrals.
Gayle was a total contrast to the first day where he belted the Sri Lankan bowling all around the ground, taking a more restrained approach during the morning session.
He moved past Brian Lara's score of 221, the highest by a West Indian against Sri Lanka and also past the highest ground score of 237 by Mahela Jayawardene [ Images ] against South Africa [ Images ] in 2004.
Sri Lanka's only success of the session was the wicket of Chanderpaul, who was deceived by the extra bounce spinner Suraj Randiv [ Images ] managed to extract. He went for an ill-advised cut shot and the edge was gobbled up at slip by Jayawardene.
Before his dismissal Chanderpaul became only the second West Indian after Brian Lara to pass 9,000 runs in Test cricket, completing the feat in his 127th match.