There are several ways of doing this.
If you know the person / people well, explain before you go that you are on a diet, this means they don't have to waste time and money making something that a guest won't eat.
If you are going to a large party where no one will care what you eat or how much or how little, then fill up beforehand on foods that are part of your diet, so you aren't hungry and tell the person offering the food, "I'm really stuffed at the minute, I'll maybe have a bit more later." They don't have to know that you ate food before you came there!
Alternatively, look at the food on offer and take what you CAN eat, then you can quite legitimately refuse what they are offering, on the grounds that you have to finish what you already have, first.
Of course, there is always the problem that you might REALLY LOVE to have some of what they are offering and are looking for help with will power or motivation NOT to eat it. Filling up ahead of time is good for this. You may also want to plan for what you will say and practice it before you go there. Something like, "Sorry, I have a bet on with my spouse / friend / whoever, that I won't eat any sweet stuff for the next 10 days. You would really help me win that bet by not offering me..." If they are a better friend of your spouse / friend / whoever than they are of you, they might want to help you lose that bet, so in that case, you say something like, "I have recently found out that I am allergic to sugar, so I haven't been taking it and when I do have a little, it makes me really sick."