The following verse is about the story of Muhammad marrying Zainab, the wife of Muhammad's adopted son.

Behold! Thou didst say to one who had received the grace of Allah and thy favour: "Retain thou (in wedlock) thy wife, and fear Allah." But thou didst hide in thy heart that which Allah was about to make manifest: thou didst fear the people, but it is more fitting that thou shouldst fear Allah. Then when Zaid had dissolved (his marriage) with her, with the necessary (formality), We joined her in marriage to thee: in order that (in future) there may be no difficulty to the Believers in (the matter of) marriage with the wives of their adopted sons, when the latter have dissolved with the necessary (formality) (their marriage) with them. And Allah's command must be fulfilled.
-- Sura 33:37

There is a considerable moral problem with such a self-serving revelation. It is quite clear that Muhammad was attracted to Zainab before Zaid divorced her and it might well have been the true reason for the divorce itslelf. There are Muslim traditions which indicate this, but the Qur'an verse itself does make clear that there was something going on before Zaid's divorce from Zainab. In any case, this marriage with Zainab, wife of his adopted son seemingly caused quite a scandal in the Muslim community as this was looked upon as incest [the wife of the son]. Therefore it was necessary that a revelation will exhonorate Muhammad for his action. What better step can be taken than declaring that this was a step of obedience to Allah's will and command? Who wants to question Allah's command?

But there is more in this strange story. That this action is immoral and this revelation / justification of it self-serving and not fitting for a true word of God is an important aspect, but not a contradiction within the Qur'an - even though contradicting the true character of God, who is moral purity.

In this verse in Sura 33:37 there is stated a particular purpose for this revelation and action of Muhammad. It is not for himself, but it is for the future of the Muslim community. It is so that in future there may not be a problem if anybody wants to marry the divorced wife of an adopted son. And that is the logical difficulty: Adoption is forbidden in Islam ([1, 2]). This prohibition is based for example on these two verses from the same sura:

Allah has not made for any man two hearts in his (one) body: nor has He made your wives whom ye divorce by Zihar your mothers: nor has He made your adopted sons your sons. Such is (only) your (manner of) speech by your mouths. But Allah tells (you) the Truth, and He shows the (right) Way. Call them by (the names of) their fathers: that is juster in the sight of Allah. But if ye know not their father's (names, call them) your Brothers in faith, or your maulas. But there is no blame on you if ye make a mistake therein: (what counts is) the intention of your hearts: and Allah is Oft-Returning, Most Merciful.
-- Sura 33:4-5

Without adoption there cannot be any adopted son either. And so the explicitely stated reason for the revelation of this verse does not even exist. Muhammad himself dissolved the original adoption of Zaid when the above revelation came.

To summarize, the logical difficulty is that Allah(?) causes a scandal and then sends Gabriel to officially justify the action with a revelation and the express purpose for this revelation is "so that there will be no problem in future" with a situation that is not even possible under Islam. Those who are Muslims and are obedient to the Qur'an will not have adopted sons.

This logical problem is a further indication that this revelation was really not from God, but from Muhammad who wanted to justify his action before the community. It was not for the future (even though that certainly sounded better) but it was to calm down the emotional upheaval and resulting doubts about his integrity because of this action which was considered indecent.[1]

1. That this act caused quite some havoc is also acknowledged at the very beginning of Maududi's book The Finality of Prophethood

Contradictions in the Qur'an
Answering Islam Home Page