The Hungarians, led by veteran fencer Geza Imre, have won the bronze medal in the men's team epee.
Hungary led the majority of the match but narrowly survived a late rally by the Ukrainians in the waning seconds of the final period.
The match started off slow as neither side scored any points in the first two periods.
Hungary went up 8-5 in the fourth behind a 6 point effort from the 41-year-old Imre.
Ukraine fencer Dmytro Karyuchenko suffered a hand injury in that bout with Imre and did not return. He was replaced by alternate Maksym Khvorost.
Imre scored 11 points in the seventh period but Anatolii Herey kept pace to put Ukraine within two points.
The Ukrainians trailed by five points heading into the ninth period as veteran fencer Bogdan Nikishin hoped to outduel Hungarian Andras Redli.
Nikishin scored seven consecutive points to narrow Hungary's lead to one point with just seconds remaining in the bout.
Taking advantage of Nikishin's necessary aggressiveness, Redli scored consecutive touches. With one second left on the clock, all Redli had to do was run to the back of the piste to ensure Hungary's 39-37 victory.
This is Hungary's first bronze medal in the event. They have won three gold and three silver medals in the team epee. Imre and teammate Gabor Boczko were on the 2004 Athens team that won silver.
This is Imre's second medal in Rio. He won silver in the individual epee after 20-year-old South Korean Sangyoung Park stunned the veteran in a come-from-behind win.