The 26-year-old paceman dismissed Hardik Pandya, Suresh Raina and Yuvraj Singh from successive deliveries before Sri Lanka, chasing 197 to clinch the series with a match to spare, were restricted to 127 for nine.
The run chase by the tourists got off to a terrible start with off-spinner Ravichandran Ashwin (3-14) luring Tillakaratne Dilshan out of his crease with the first legitimate delivery and Mahendra Singh Dhoni removing the stumps in a flash.
Sri Lanka were then reeling at 16 for three before Dinesh Chandimal (31) and Chamara Kapugedera (32) staged a rally.
Milinda Siriwardana (28 not out) and Dasun Shanaka (27) also chipped in with useful knocks but they proved in vain in Dhoni's home town.
Smarting from their humbling in the series opener in Pune, India made an excellent start as openers Shikhar Dhawan (51) and Rohit Sharma (43) shared a quickfire stand of 75 in seven overs after Sri Lanka had opted to field.
Dhawan, who survived a strong lbw appeal early on, hit three successive fours in Kasun Rajitha's second over and took only 22 balls to race to his maiden international half-century in this format.
Dushmantha Chameera then had Dhawan caught behind before he took a brilliant diving catch off his own bowling after outfoxing Sharma with a slower delivery.
Boundaries temporarily dried up but, with five overs remaining, Pandya (27) hit spinner Sachithra Senanayake for back-to-back sixes and Raina (30) struck four boundaries in one Chameera over.
Perera then had Pandya caught at long on, Raina at backward square leg and the scoreless Yuvraj at long-on to become the fourth man to grab a Twenty20 international hat-trick, following Australian Brett Lee and New Zealanders Jacob Oram and Tim Southee.
Visakhapatnam stages the final match on Sunday.
Original source: Perera hat-trick in vain as India level series