Hi,
Mains sense can be done in many ways.
You can use a bridge rectifier at the input and use this to turn on an optocoupler (4N35, etc). Use the output from the optocoupler to control the relay or control circuit.
You can also use specialized optocoupler like TLP620 which can directly be driven from mains as there are two "LEDs" inside this optocoupler and hence both mains phases can be detected.
Or you could rectify the AC to DC, use a voltage divider and then feed this into a transistor.
This is the mains sensing part. Each of these will give either a 0 or 1(5 or 12volt, depending on your circuit) depending on your circuit configuration. What you must next do is, decide how to use this signal to switch to battery supply.
You could use a relay, which would be easiest, such that the relay turns on when mains is absent, and consequently turns on the oscillator when mains is absent.
Hope this helps. If you require more help, I can try to explain further.
Tahmid.