Is it possible to extract a hex file via UART from an Arduino?
AVRDUDE which is used by the IDE to send the content of HEX file from your computer, as a feature letting it to get back the data of the flash, in order to compare and see if data are correct.

For a UNO try:

avrdude -patmega328p -carduino -P/dev/cu.usbmodemFD121 -b115200 -U flash:r:"flash.bin":r 


I think it would not be very hard to change that for a Mega.

It works fine. I have use that a lot to debut my "Android Updater" which allow the user of my board to update the code themselves.

PS: Just for info, you'll not get back the HEX file as in the flash, you have only some "pieces" of the HEX file.