Basic rule without ال:
مَرَرْتُ بِمَسَاجِدَ — مَسَاجِدَ is genitive with fathah.
Important exception with ال:
مَرَرْتُ بِالْمَسَاجِدِ — الْمَسَاجِدِ is genitive with kasrah.
Why does the ending change? The basic fathah-in-genitive rule applies when the ghayr munsarif noun has neither ال nor idafah. Once ال enters, kasrah is used in the genitive.