I don't think those 2 CPUs use the same socket, but having said that, I have no idea whether they are able to be changed on a Mac. I have never had an iMac until the 2021 model I just got my wife and I have no need or desire to open it up. I am led to believe though, that opening an iMac of any vintage is quite the endeavor.
Look up both CPUs and see what socket each uses. I think my results were "LGA 1156" and an "LGA 1155" so to me, these are different but I don't own any systems that old so can't say for sure.