They don't really have a gender. Some words use "il"/"lo" as their definite article and other words use "la".
You could simply call words that use "il"/"lo Class 1 nouns, and words that use "la" Class 2 nouns.
As uomo and ragazzo happen to be in Class 1, and donna and ragazza in Class 2, people in the past decided to label the entire classes as "masculine" and "feminine".
They could have instead decided to call the classes O and A, or "libro" class and "colazione" class, or whatever, and it wouldn't change anything - except generations of learners wouldn't have wondered whether Italian breakfast is girly.
Why are there two classes of nouns at all, well, because languages such as Italian weren't designed but evolved from previous languages and it just turned out like that.
And having grammatical gender may be useful to disambiguate between words that otherwise sound similar, or to refer to things with "he" and "she" without repeating yourself.