Sheikh `Abd al-Rahmân al-`Ajlân, lecturer at the Grand Mosque in Mecca
Buying and selling in the mosque is disliked. The Prophet (peace be upon him) said: “If you see someone engaged in buying or selling in the mosque, then tell him: “May Allah not bless your business” [Sunan al-Tirmidhî (1321) and Sunan al-Dârimî (1401)]
Al-Tirmidhî considers it to be a good but strange hadîth and al-Dârimî mentions that a number of scholars found nothing objectionable about it.
Though there is more than one opinion on the matter, the strongest opinion is that buying and selling in the mosque is disliked, on account of the hadîth.
However, if someone sells something there, his sale is valid, since it fulfills all pillars and conditions and there is no factor present that can make the sale legally void. The ruling that such a sale is disliked does not have the legal consequence of making the sale invalid.
The fact that the Prophet (peace be upon him) said: “May Allah not make your business profitable” without mentioning anything about the sale being invalid, is indicative of the validity of the sale.
And Allah knows best.
Source: Islam Today