I couldn't figure out the grammar rule here. I know it is possessive pronoun but I don't know the case (it seems nominative here)
Sie kauft die blaue Mütze für ihren Bruder.
Please tell me why it is ihren here?
In general, you should be able to determine the case by searching for the preposition in some dictionary or even on Google.
Actually, knowing the case taken by a preposition is is not complicated at all if you have a good memory (or have practiced enough).
By the way, this wikibook has a decent lesson about prepositions and cases.