I've looked into this a little bit, but wanted to see if anybody had any good details.
What happens if 20 years from now you renounce U.S. citizenship and take up residencey/citizenship in another country?
How are your taxable and non-taxable accounts handled when you leave the U.S.?
If you plan to do this in 20 years, would you be better of putting money into a taxable account and doing tax-gain harvesting up until the point you leave the US?
First off, for all the haters in this thread: you clearly haven't ever had to deal with filing taxes in two countries. All the americans I know have to pay hundreds of dollars ever year to a professional just to get their taxes filed because of the nightmare of combining two tax systems. (I.e. local country taxes first, then you need to figure out US liability.) And most of them end up paying thousands to the US for the privilege of... I don't actually know what.
It gets worse: if you make mistakes, or forget to file the right forms (FBAR anyone?) you can get fined.
Now, for OP:
The answer is, it depends. Specifically: it depends on what country you're withdrawing in, and more significantly: the dual tax treaties between the USA and country X. And finally: it depends on how taxes in country X work.
If you're lucky, country X and the USA might have a dual tax treaty that only permits country X to tax the 401k/IRA withdrawal. In which case: you only pay taxes in country X. The US would still withhold a certain percentage of the withdrawal (default of 30%, can be smaller for residents of certain countries) - but they return it when you file the relevant tax return.
If you're unlucky, there's no agreement, and both the US and country of residence tax the withdrawal. The US would still withhold a flat 30%, country X could have any arbitrary tax system.
In general, most dual-taxation agreements with the US seem to cover 401k's and IRA's (but the details vary depending on country) - so that's not something to worry about too much. Roth's (Roth IRA, Roth 401k) however often aren't covered, which makes it impossible to give any clear answer.
But finally: you may need to watch out for that exit tax BS. Apparently if you have too many assets the US will charge an "exit tax" when renouncing (or even when giving up a green card under certain circumstances).
// Edit: for an example of countries - I looked up New Zealand, and (A) the US-NZ tax agreement only let's New Zealand tax withdrawals of NZ residents (except for US citizen ofc), and (B) NZ apparently doesn't charge any tax on retirement withdrawals within a 4 year transition period. That sounds like a dream place for anyone who's resided and worked in the US for a while.