If the clock on your Mac starts to lose time and you start getting weird results then it is a good sign that the motherboard battery is failing.
The first thing that you need to know is the type of battery.
It is a Tadiran 1/2AA 3.6v 950 MAH Lithium (TL5101) or equivalent. It looks like an AA battery cut in half. The battery is easy to get from a good electrical store and costs between £4 and £12 depending upon where you get it. I used a Maxcell ER 3S-TC which I got from Maplins in Northampton.
Replacement of the existing battery is a little awkward but fairly easy.
Work on a table at a reasonable height. Ensure the IMac is switched OFF. Take the plug out of the wall. Put a cushion or rolled up non-static cloth on the table and put the IMac on it upside down.
On the underside of the IMac there are two entry hatches. One is for an external VGA monitor, apart from the socket there is nothing else underneath it. The other slot has a circular coin shaped fastener. Put a small coin in it and turn it to open. Inside you will see one or more memory SIMMs.
The battery is on the screen side of the opening and on the left. It is in a standard battery container. Use a small torch to view the battery.

Showing position of the battery
Ensure that you ground yourself before putting anything in the opening. Using a pair of long-nose pliers grab the battery and pull it up. It may need a bit of a tug. Check the orientation of the battery and note which end is positive (+) and negative (-).
To replace the battery simply place the new on top of the battery container with the long nose pliers, then put your finger on the battery and push it down. Get the orientation right. Negative is towards the screen.
Close the hatch. The whole process takes a couple of minutes.