While it's true that fezzes as we now known (And in the form, say, Aladdin himself wears one) and as a part of Ottoman official dress only became a thing much later, the fez as a piece of clothing date back much further. Mehmed II is very famously depicted wearing one. (As in, the depiction of him is famous, not that the fact that he is wearing a fez is a famous fact about the depiction)
"BUT KELD. THAT'S A TURBAN."
I can already hear you protest. Yes, you're right. A turban wrapped around a fez. Because that's how fezzes were worn initially.