The North African side fell behind to a 33rd-minute own goal from Yassine Meriah but battled back to score twice in the second half through Fakhreddine Ben Youssef and Wahbi Khazri.
Tunisia, ranked 21st in the world, finished third in Group G behind Belgium and England.
"Today's match was a well-deserved victory for the Tunisian side," said coach Nabil Maaloul.
"When we landed in this group we knew very well that our best hope would be to come third in this group. It is true that we conceded many goals but we conceded goals when we were met with very tough opponents."
Tunisia, who have appeared at four World Cups before the current tournament in Russia, last won a match at football's showpiece event in 1978.
Panama finished bottom of Group G after three defeats in their last ever World Cup.