Upsetting as it is, people will do this. And not just border officials. I was rushing to make a Canada-US connection, and had my passport in my hand with my boarding pass, when I came upon a TSA security point I wasn't expecting. The attendant asked for my boarding pass and when I held it towards her, grabbed everything out of my hand. Then she wouldn't give me back my passport. I stood in place and kept saying "you can have my boarding pass, but not my passport" and she kept saying "you can have it back on the other side". We were a line of 5-10 people who had a late incoming flight and were hurrying to make our connection, and she knew it. Finally she said "do you want to make a point, or do you want to catch your flight?" and furious, I went through, whereupon she gave it all back to me. I have no idea what the heck it was about, but I do know that if my passport had disappeared into her pocket while I walked through the metal detector, a thousand supervisors would have all said "oh no ma'am, our screeners never take passports, only boarding passes." Even knowing that, I gave up arguing because (a) I felt the chances were slim the screener was actually trying to steal my passport and more likely that English was her second language and she was misunderstanding my demand for the PASSport as the boarding PASS, or she was embarrassed to have made a mistake and was digging her heels in to cover the error and (b) I hoped my fellow passengers had all witnessed the incident and would help me get my passport back or whatever might need to happen if she had kept it. Similarly I think in most of the cases where people take your passport away, they will bring it back. Most.
Whenever I have needed a visa-at-the-border or anything more complex than lining up at a counter and being stamped on the spot, the passport has sometimes gone out of the room. I have always got it back. If there are laws, I don't see how they could be binding. That is, if one particular country passes as law that says "our passports can't be out of sight of their owners", how will they ensure that other countries follow that law?
I really hate leaving my passport with hotels etc (and don't understand what the purpose of the practice is) and have had reasonable success just saying "no thankyou" as though they were just offering to keep it safe, or "thankyou, but I need to bring it with me to my meeting in the morning". Not 100% success, but enough that it's my usual behaviour to keep it rather than leave it.