This site defines famous last words as
This expression is used as a way of showing disbelief, rejection or self-deprecation.
"They said we had no chance of winning- famous last words!"
And I think it's fair to say that's how it's often used. More literally, Cambridge online defines it as
said when someone makes a definite statement which is shown very soon, and in an embarrassing way, to be wrong. "I told him categorically that we could never be anything more than friends. Famous last words! Within a few months we were engaged."
It's interesting to consider one of the most famous last words of all - the Antarctic explorer Captain Lawrence Oates saying:
"I am just going outside and may be some time"
where Oates clearly knew as he said this that he was being at best misleading, since he did not intend to return (his body was never found). But it does carry one common aspect of the idiomatic famous last words - things often go badly for the person said to have uttered them. Sometimes "go badly" just means something as trivial as being proved to be absurdly, exaggeratedly wrong. But often the bad consequences can be far, far worse.