I have seen the questions here and here, but my reasons for wishing to withdraw are utterly different than theirs, so I don't think they're especially relevant.
I work in mathematics, say in field A. In recent years, field A has found a number of interactions with another part of mathematics; call it field B. About 7 or 8 months ago I finished a project that was an application of some results from field A to a question in field B. Despite using some results from field A, the resulting paper (both in techniques and scope) was essentially purely a paper of field B, and so I submitted it for publication in a journal J from field B.
Last week, while working on a completely unrelated project in field A, I realized that the techniques I had used in the abovementioned paper can be translated, essentially word-for-word, to get some results in field A. Unfortunately these results are no longer at all in the scope of journal J, as they are really applications to field A rather than field B. Thus, I thought the right thing to do would be to withdraw the submission from journal J, merge the two results into a single paper, and then publish in a journal that lies more in the intersection of field A and field B. So a few days ago I emailed journal J to inform them of the situation and request withdrawal of my submission.
Unfortunately, I think that email has not yet arrived to the editor handling my submissions, because I have now received an email informing me that my paper has been accepted to J modulo minor revisions and enclosing two referee reports. The timing is really unfortunate; I wish I had realized the application to field A a few months ago rather than just last week.
I still think the right thing to do is to withdraw the paper. However, I feel terrible for wasting the time of the journal, and especially of the two referees, who have put a great deal of time into reviewing the paper! Is it unethical to withdraw? More generally, is it insulting to the referees? If so, what is the best way to handle this situation?