Sarcasm is tricky. You correctly state that the written word is a poor medium for sarcasm (and surely you can't put your retorts betwen tags, unless if you hope to become a meme on some obscure internet imageboard).
Yet I'd argue that context can help you make clear sarcastic remarks, even when you can't portray vocal inflections (let's ignore that in a novel you can still describe those, but that's a roundabout).
The Dictionary.com entry on irony states that:
In sarcasm, ridicule or mockery is used harshly, often crudely and contemptuously, for destructive purposes ...
So, first of all, the intent of sarcasm is to make destructive irony. If we take this from granted, we have a pretty narrow scope of what the place of sarcasm is. What meaning should be conveyed.
Let's say you are arguing with a person on the internet. After an hour long heated debate, you decide to end your sentence with a:
"Wow, you must really be a genius in your field."
It's clearly a snappy remark, and sarcasm at its base level. Most of the readers will get the derogatory intent of your words. The literal meaning of the sentence would seem to point to a compliment, but this interpretation would clash against the whole discussion (hence, it would clash with the context).
Also, as Amadeus noted, absurdity can be used as a clue. In my example the sentence is a little over the top (most people don't use "wow" seriously in a sentence). And again, the literal meaning of the sentence would be absurd given the context of the heated discussion. As humans, we know well that if two people are arguing they are more likely to insult each other rather than the opposite, so ...
I'd argue that in literature is easier, since the narrator has the tools to make the contrast even more sharp. The PoV character might struggle with some task; the author can indulge in how difficult it is paragraph after paragraph. Once you set the context, when someone suggests something that is clearly absurd about what has been stated, it's clearly sarcastic.
Maybe it's the classic young farmer learning to wield a sword. When the rival nobleman comes in and says "Did you manage to stick them with the pointy end?" the sarcastic intent will be clear. Most readers will be able to pick that up.
So, TLDR, the elements that can help you are:
- Derogatory intent
- Absurdity of the literal meaning
- Being a little over-the-top
I don't really like the last option, but the again, there it is.
I don't want this to become a mile-long answer, but I'll add some final statements here. In my opinion, the aim of sarcasm is not necessarily to be understood.
If you're being rude to me and I want you to know that I don't appreciate that, I may state it out loud. I may even insult you to show my dissatisfaction. But if I want to mock you, I can simply say:
"Well thanks dearly for your kindness."
Of course there's a chance that you won't pick up the sarcasm, but that's part of the game. I have succeeded in the intent of mocking you. This kind of sarcasm can go completely undetected. I do tend to use this form of sarcasm/irony, and deliver my jokes in a completely flat tone: the vocal inflections are cues, but they are not necessary to sarcasm.
The same actually happens in internet. If you are writing a sarcastic piece and someone takes it seriously, the joke's is usually on them (and I stop here before I go rant about memes).
Of course this doesn't apply well to novels and literature; while there are works that employ a similar use of irony (e.g. A Modest Proposal, to cite a satyrical classic) a sarcastic comment in a novel will work if most of the audience will be able to catch it up.