What is the etymology of 居然? Usually X+然 has some kind of meaning related to X, especially archaic usage. However I do not see any relationship between 居 "to reside" and "unexpectedly". Are there any historical sources that explain where this expression came from?
out of my expection. I have never thought.

There is a famous 对联


  客上天然居,居然天上客


There are customers visiting the Natural House, Unexpectedly, They come from the Heaven.