Hello,
I fit only happens once, then I probably wouldn't panic. If it happens again, then it may be a problem.
There are two common reasons for an iMac to shut-down:
1) a failing power supply.
2) Over-heating.
If the power supply is failing, it can cause random shut-downs (as in just turn's off).
As for over-heating, all the recent Macs have a protection mechanism built-in. If the system gets beyond a given temperature, the computer will turn itself off (without warning) to prevent permanent damage.
So, if you notice that the computer is running warm before it shuts-off, then that could be the cause.
But, it is also very likely to be the power supply.
And, operating with a faulty power supply could lead to tons of other problems or damage.
So, if it happens multiple times, then I would personally get it into a shop for inspection and repair very fast.
I hope this helps.
Let us know if you have other questions.
P.S., if you'd like, go ahead and click the "Helpful" or "Solved" buttons on any of the posts / replies above if you feel they were helpful or adequately answered your question.