Copil, 28, ranked 93rd in the world started the final as the overwhelming underdog.
But he had shown teeth during the week in Basel. He emerged from the qualifiers into the main draw and then dispatched top 10 fixtures Marin Cilic and Alex Zverev.
He traded breaks with Federer before the local hero snaffled the tiebreak seven points to five.
But Federer took his foot off the gas early in the second set allowing Copil to establish a 4-1 advantage.
But the veteran clawed his way back to 4-4 and broke again to lead 5-4.
The title seemed imminent when Federer surged into a 30-lead. But Copil extended the suspense with three straight points to hold a break point.
Federer saved it and wrapped up the victory to claim the crown for the ninth time.