Treinta y uno, cuarenta y uno, etc.
I know it's right to answer "treinta y uno" if somebody asks me "cuantos años tienes", but "tengo treinta y un años" is also a right way to answer the question.
Same applies to "veintiuno" and "veintiún".
This difference only happens with the "uno". For all the other numbers is stays unchanged. By example, if you are 31 years old, you answer "tengo treinta y dos" or "tengo treinta y dos años".
I'm curious about if there is any gramatical rule/explanation about why this happens.
I cannot give you an authoritative answer, but my guess is that in Indo-European languages we have the determiner "a/an" (or equivalent in other languages), which also means "one", but we don't have similar thing for two, three or more. These two words clearly overlap, since they often mean the same, but they are also grammatically different, so it is not surprising that we have different rules for numbers considered as nouns or pronouns, using "uno" (eg. tengo uno), and numbers considered as adjectives, using "un" (tengo un gato). Have you never heard a foreigner trying to say "Tengo uno gato"?
In English, "a" and "one" have the same function very often, as in "I have a cat" / "I have one cat", but while you say "What a day!", you don't say "What one day!".
You can (one normally does) shorten "uno" to "un" when it directly precedes a masculine noun. However, in the case where your answer is just the number (e.g. "treinta y uno" instead of "treinta y un años") you need to revert to the full form "uno".
I am not sure, but I would imagine it's for the same/similar reason that a + el is made into al. If one were to say "Voy a el parque" .... it would be cumbersome and usually sounds like "Voy al parque" anyways. SO the a+el became al. I think it's because the vowels are next to one another and would blend. If you say veintiuno años quickly, it becomes veintiún años....
Anyway just my guess...