With leaders AC Milan having beaten Atalanta 2-0 at the San Siro earlier on Sunday, Inter had to win in Sardinia to keep the title race alive, edging in front in the 25th minute through Matteo Darmian.
Martinez added a second six minutes after the break for Inter and they appeared on course for a comfortable ninth win in 10 matches in all competitions, before Charalampos Lykogiannis arrowed in a strike out of nowhere to get Cagliari back in it.
Inter suffered some nervy moments before Martinez's quality shone through at the last, as his classy finish six minutes from time finally ended Cagliari's hopes of a comeback.
The victory ensured Inter remained two points behind Milan going into the final round. The champions must beat Sampdoria at home on Sunday, and hope Milan lose at Sassuolo, if they are to defend the Scudetto.
"It wasn't easy after Milan's win, but the team played really well against an opponent who is problematic on their own ground," Inter coach Simone Inzaghi said. "I congratulate my team, because this was by no means to be taken for granted, especially after playing 120 minutes on Wednesday [in the Coppa Italia final].
"Naturally, we are behind, so we need a victory and for them [Milan] to lose. It's happened before, I won with Lazio when we were two points behind and Juventus lost to Perugia [in 2000], so it can happen again."
The visitors did not have to wait much longer for one to count, however, as Ivan Perisic crossed for Darmian to head home his second league goal of the season.
Martinez almost added another on the cusp of half-time, before hammering home the second -- his 20th league goal of the campaign. The Argentine became the sixth player in Inter's history to score 20 goals in a single Serie A term before turning 25.
In cruise control, Inter stood off Lykogiannis and his strike was unstoppable, giving the home fans hope.