The cost is undone if it is undone by whatever card is undoing the benefit.
Otherwise, the cost is sunk.
For Egyptian Warden, it usually won't matter; EW usually causes SI, and an ITB ability will interrupt the last card played if it was used by an opponent. This interrupts all of EW, and the discarded EE comes back.
For the Elymas example, the cost was an instant ability, so it is not undone by an ITB ability if you played Help From Heaven after Elymas activated. The demon stays discarded.