Because its origins are Greek
I had a Spanish professor (who was Greek) years ago who explained that, just about any word with a “-ma” ending was a Greek word and should be treated masculine.
That doesn’t mean the word in Greek was masculine, though
Greek has three grammatical genders. And πρόβλημα, (provlema) is in fact a neuter word.
It’s one of those fun little “exceptions to the rule” that you’ll have no choice but to memorize as a non-native.
But, why
In John McWhorter’s book, “The Language Hoax”, he remarks:
…that’s from a study that showed that speakers of languages assign gender to inanimate objects are statistically more likely to imagine them with traits corresponding to the “sex” they belong to.
There’s even some support for this because in gendered languages, the adjectives we used to describe concrete nouns will be more masculine or feminine based on the assigned grammatical gender of the noun. i.e. German bridges are beautiful and Spanish bridges are strong.
But that might be a stretch for abstract concepts like “problema”. But there may still be an explanation for how a neuter word ends up masculine in a language that doesn’t support neuter:
…experiments have shown that, as we might expect, the default tendency is to associate supposedly gender-neutral pronouns with men
So, with pronouns at least, we have a tendency in European languages to assume neuter pronouns are masculine. So it could be possible to see how we my take the same concept with nouns, too. So neuter Greek nouns just make sense as Spanish or Portuguese masculine ones.
Add in maybe a little cognitive bias with Greeks seeing unsexy, eunuch-like problems without nary a Pythagoras in sight, and maybe it makes sense how it went that way.
But, cama?
Notice how problema ends in “ema”. That’s the feminine part. Cama doesn’t end in ema so it’s not a ‘-ma’ rule but an ‘-ema’ rule.
Because even exceptions need exceptions.