The reason why this error comes up is because the programmer cannot communicate with the bootloader of the ESP.
The link you mentioned in the comments misses a very important step, which is to remove the ATmega chip already on the Uno board. Otherwise the programmer will see corrupt data due to 2 bootloaders on the same UART lines.
Remember that only one device can communicate on the UART lines.
The Mega board has the ATmega chip soldered to the board, so you don't have much hopes of isolating the UART lines.
Another thing to remember is to always reset the ESP into flash mode (reset the ESP with GPIO_0 pulled low).
If you still want to use the ESP with the Mega board, go through the Multi Serial examples given in the IDE. The concept requires making two GPIO lines communicate with the ESP, while the original Serial lines to the programmer (the computer).
All data sent via ESP is routed to Mega's serial lines and vice versa.