The situation you describe is called MultiMaster. It is normally done in hardware using the I2C peripheral in the MCU. However the ESP8266 doesn't have one (or if it does, it's not used), and uses bit-banging to implement I2C in software. If you can add MultiMaster functionality to the ESP8266's Wire library then possibly yes, you could do it.
Another option is to have some other communication channel between the two masters (it could be as simple as a wire with a pullup resistor - see below) so that only one master communicates at a time.
Electrically there is no problem with your setup - I2C is a fully open-drain system (or should be implemented as such - hence the pullup resistors needed on the SCL and SDA pins), so multiple devices can drive the lines at the same time. It's just that if they do you will get data corruption and communication won't happen.
Example: Semaphore Wire Implementation
- Connect any two GPIO pins together with a wire. Add a pullup resistor to the wire (10kΩ should suffice).
- Both GPIO pins are set to INPUT.
When a master wants to send it first reads the GPIO pin:
- If it is HIGH then it is "clear to send". So it sets the GPIO to OUTPUT and pulls the line LOW.
- If it is LOW then it waits for a random amount of time and tries again.
Once communication is finished the master sets the GPIO pin to INPUT again.