I would like to highlight another angle of this matter.
At the time when the verse limiting the number of wives to 4 was revealed, Muhammad (pbuh) had more than 4 wives.
He was given special dispensation by Allah to keep his wives as many as they were at the time, though he could not take any further wives after that verse.
It is hypothetical but I presume that he would have been allowed further wives up to 4 if the number of his wives had dropped below 4 due to death of his wives, after revelation of this verse.
His case was special for a number of reasons, in my understanding.
Unlike other women, his wives were not permitted to remarry after his death. This prohibition was from Allah.
As far as I know, he never divorced any of his wives.
While Muslim men must establish turns for each wife, he was permitted to cancel the turn of one of his wives if he wanted to. As far as I know, even a Muslim woman may not cancel her turn if she wishes. This is part of establishing justice between wives.
Finally, Allah Knew that He was going to end the line of Muhammad (pbuh) in order to ensure that no doubt remained regarding the finality of his prophethood. This ties in with Allah allowing Muhammad (pbuh) to keep his wives rather than divorce them even though he had more than 4 wives because:
It would have been unfair to the wives he did divorce and those wives were then not permitted to remarry.
If they had been permitted to remarry, then there was a chance that they might have had a son and later people might have considered that male child to be continuing the line of Muhammad (pbuh). This could have caused ambiguity in the matter of the finality of prophethood.
Surat-al-Kausar is related to this matter in which Allah Consoled Muhammad (pbuh) regarding not having sons who survived.
May Allah increase our understanding of our religion. Allah Knows Best.