I recently learnt at school that when matter and antimatter collide, they annihilate each other and the amount of energy released follows the equation $E = mc^2$. If there were two planets (one made out of matter and one made out of antimatter) that collided, what would happen to objects in their gravitational fields? For example, the moon must have gravitational potential energy due to being in the Earth's gravitational field so how would this energy be conserved if the Earth were annihilated? I thought that perhaps the energy left afterwards would exert the same gravitational force as the Earth does at the moment but my teacher told me that only matter can exert a gravitational force. So, how is the energy conserved?
Energy exerts also a gravitational force, but typically the amount is very small so it is ignored. For your example, there would be a huge amount of energy there, which exerts the same amount of gravitation as the two planets before.
However, be aware that the energy would not be staying in the same spot - the explosion would quickly dispers it over a large volume (and for your example, any moon circling either planet would be blown apart in milliseconds, as would be everything with some lightyears of range). But even after the energy has spread over a cloud of hundreds of cubic lightyears, it will still exert the same gravitational force. Just again, the gravitational force of the dispersed energy from the two planets over many lightyears is miniscule.
but my teacher told me that only matter can exert a gravitational force.
Your teacher is talking in the framework of classical gravitation and mechanics.
Now annihilation can happen within the frameworks of special relativity , for conserving energy, and quantum mechanics, for particle antiparticle annihilation.
There is a lot of physics to be learned to be able to understand nature as we have studied it, so as to be able to predict situations as you describe and have been answered in the other contribution.
If the teacher wrote E=$mc^2$ and he later said energy does not gravitate he does not understand relativity, nor the principle of equivalence.
A bit of an expansion on @Aganju's good answer.
If the earth and an anti-earth aninhilated all kinds of particles and radiation would come out. Simplistically, those, in the instant before they disperse, would have the Moon exactly feel the same gravitational force before anihilation (which by the way would include the potential energy of gravitational attraction from both the earth and anti-earth).
You'd also have to account for the gravitational potential energy between the earth and anti-earth, as that is part of the total energy in earth-anti earth system. There's other minutiae to be included like the earth-anti earth kinetic energy (e.g., total kinetic and potential count, as well as their mass). In any event the earth-anti-earth explosion would probably destroy the Moon and since radiation has momentum also push the remnants of the Moon out. Lots of bad thingsto account for. But in the basic statement, your teacher is wrong.
(a) Strictly speaking, no one knows the answer to your question. That's because matter/antimatter annihilation is a quantum-mechanical process and you are specifically considering the regime where the annihilating matter's gravitational back-reaction is important, so answering your question would require a full theory of quantum gravity, which we don't have. It's conceivable that the gravitational effects could qualitatively change the nature of the annihilation process. That having been said, we believe that as long as the curvature remains small enough that we are far forming a black hole, the general people that people are describing will remain valid, where the released energy gravitational attracts itself.
(b) The concepts of "gravitational potential energy" and "conservation of energy" are notoriously subtle in general relativity, and in fact there is no general formulation that always applies. So you need to be careful about using any intuition that stems from conservation of energy.