Pierre-Emerick Aubameyang hit his third goal in three games to set off the co-host's celebrations at Stade de Franceville after its third straight win at the tournament.
The striker collected a pass from Daniel Cousin in the 62nd minute, cut inside and shot past goalkeeper Rami Jeridi via a deflection to separate the teams in a tight group decider.
Gabon's entire team danced in a huddle and even coach Gernot Rohr, who has remained calm throughout the country's giddy group campaign, allowed himself a smile on the sidelines.
Both countries had already qualified for the quarterfinals but Gabon's victory ensured it topped the group and is now likely to avoid a last eight meeting with Ghana - which is expected to clinch first place in Group D.
Mali and Guinea are the other teams in contention for the quarters from that group.
''I don't have any preference who we play in the quarterfinals,'' Rohr said. ''We're just happy to be there. We weren't expecting to be first (in the group) but we are first.''
Gabon President Ali Bongo and wife Sylvia smiled and applauded Aubameyang's goal at the stadium, with the first lady once again putting her faith in the in-form striker by wearing a bright yellow team shirt with his No. 9 on it.
He clinched the game when he controlled the through ball from Cousin, only just kept his footing on a sodden pitch, and sent his shot off Tunisia defender Bilel Ifa and bouncing over a diving Jeridi.
''We wanted to win this match to go back to Libreville (for the quarterfinals) confident and we succeeded,'' Aubameyang said.
Aubameyang's strike sparked a frantic period of pressure from the home team. He hit another low shot in the 67th that Jeridi scrambled away unconvincingly. Cousin was only just beaten to the ball by Aymen Abdennour a minute later, after Jeridi had again parried.
Tunisia - which dominated the first half without reward - was suddenly stung into action in the last 20 minutes, with Ifa hammering a shot straight at goalkeeper Didier Ovono after he was set up from a short free kick.
After a desperate scramble in the Gabon penalty area in the dying minutes, the ball fell kindly for Ovono and Tunisia's last chance slipped away.
''I'm happy even though we lost,'' captain Issam Jemaa said. ''We tried to score and tried to win and now we have to think about how we can get to the semifinals.''
Both teams made a host of changes for their final group game, with Gabon resting forward Eric Mouloungui and replacing him up front with Cousin, who captained the team.
Tunisia left goalkeeper Aymen Mathlouthi and captain Karim Haggui on the bench among its eight changes ahead of its quarterfinal - which now looks like it will be against title contender Ghana.
''We don't think about the name of the teams we play,'' coach Sami Trabelsi said, ''Tunisia has the quality and confidence to be able to face any opponent.''
Urged on by fans, Aubameyang found space on the right wing on 21 minutes and his low cross was scrambled away by Ifa with Cousin looking to force it home in Gabon's best chance of the first 45.
Gabon picked up where it left off in the second as Edmond Mouele's 54th-minute shot from the right was deflected just wide by Abdennour with goalkeeper Jeridi flat-footed.
Gabon: Didier Ovono, Edmond Mouele (Remy Ebanega, 73), Bruno Ecuele Manga, Lloyd Palun, Pierre-Emerick Aubameyang, Daniel Cousin, Bruno-Zita Mbanangoye (Georges Ambourouet, 90), Andre Biyogo, Moise Brou Apanga, Roguy Meye (Levy Madinda, 45), Charly Moussono.
Tunisia: Rami Jeridi, Bilel Ifa, Houcine Ragued, Youssef Msakni, Khalil Chammem, Wissem Ben Yahia (Yassine Chikhaoui, 70), Issam Jemaa (Sami Allagui, 70), Anis Boussaidi, Saber Khalifa (Oussama Darragi, 83), Aymen Abdennour, Jamel Saihi.