The word autopsy is older, and had the original meaning "to see with one's own eyes". The Oxford English Dictionary writes that it was first used in a now-obsolete sense in the 1600s:
The action or process of seeing with one's own eyes; personal observation, inspection, or experience. Now rare.
Its current use only dates from the 1800s:
Med. Examination of the organs of a dead body in order to determine the cause of death, nature and extent of disease, result of treatment, etc.; post-mortem examination; an instance of this.
1805 Philos. Mag. 21 240 The distinguishing signs of peripneumony and pleurisy are so uncertain that they have been doubted by some celebrated physicians; they have been so often belied by cadaverous autopsia.
Necropsy is a much more recent coinage, and dates from the 1800s:
1842 R. Dunglison Med. Lexicon (ed. 3) 470/2 Necropsy, autopsia cadaverica.
Note that in the example given, autopsy cadaverica and necropsy are synonymous. However, because an autopsy could have been the inspection of any body, it was used in the phrase "cadaverous autopsy" or "autopsia cadaverica". It seems that necropsy may have developed as a one-word term to describe the autopsy of a deceased person, though by the 1830s autopsy was being used on its own in its current sense:
1830 Foreign Rev. 5 502 Two days after the autopsy, which was performed immediately on the emperor's decease, the whole body turned yellow.
However, it can be argued that the 1830 example was really referring to an autopsy of a body in general, who happened to be deceased (as there is qualification within the sentence which refers to death). Then, the first use of autopsy on its own without such qualification would be in the 1880s:
1881 Times 22 Sept. 4/1 The physicians' autopsy [of President Garfield] shows the bullet to be nowhere near where it was supposed to be.
From this, I think that necropsy developed as a one-word synonym to cadaverous autopsy, and then over time autopsy came to be synonymous with necropsy.