I'm basically "the IT-guy" for family and friends and have thus both seen and talked about quite a lot of stuff that is, well... controversial.
A gentle reminder
Most important:
Always remember that it's their device and their way of dealing with the information stored on it! Don't try to preach anything related to privacy or you'll be tilting against windmills. Just give them a small reminder that it's possible to clear parts or the entire history, if they want to.
A small chat about how websites are able of identifying a user is usually more than sufficient to kick of a discussion on privacy which you can use to offer them help with managing their browser-history. With good friends - or people of whom you know that they don't mind talking with you about the topic - you could try being upfront:
When I was working on your I noticed you had in an open tab/your browser-history. Mind if I show you a few tricks with your browser?
In general especially younger people are a lot more open about this kind of topic and won't mind talking about explicit content (Note: this is personal experience, localized to middle-Europe; this might not apply to Nepal(?)). While this should do to deal with situations where it's already too late for being up-front on what you've seen this kind of situation should be prevented in the first place.
Privacy and trust
Keep in mind that friends showing you their device put some trust in you. They most likely don't mind you seeing their history, but might be more picky about other people. Remind them that what is visible to him and you is visible to anyone else who gets the phone into his hands should do.
Or depending on how tech-savvy they are they might not even realize what they are actually showing to you. Clarifying what you will see when working with their device beforehand might in some cases make this whole issue a lot simpler (and should be done irrespective of circumstances, unless the person has sufficient knowledge to judge what you'll see, IMO). In that case make sure though to tell them that you'll treat anything on their phone/PC as confidential and that you can show them how to get rid of anything in their history if they want you to.
In either case, at least a short word on what you'll do and see to the owner of the device won't hurt and is a fair chance for them to choose what you'll see (and also for you - see below). Keep this appropriate to circumstances, like tech-safeness of the owner of the device, type of content you'll work with, etc..
How to deal personally with the content
Now you may not feel comfortable looking at specific parts of their history/open tabs. In that case you should point that out instead. A generic phrase like
Keep in mind that I'll see what you're doing on that phone. I feel a bit uncomfortable looking at browser-activity that is not my own, so if you don't mind, please close any open tabs and clear your history.
will get you a device in the state you'd like and/or may start a conversation about the topic, which should also help your purpose.
Legal
This should be mentioned under any circumstances, if such a situation takes place:
Your friend/colleague hands you his work-device, issued by the company, with NSFW-content open or in the history.
Alongside any company-issued devices usually comes a contract on how to use them. It is common use to restrict the usage of these devices to work-related things and any violation might have serious consequences depending on how the company deals with breaches of that contract (worst-case: you're fired). Telling them to be careful with their history or refrain from visiting any NSFW-content on that device (and telling them that you'll handle what you just did/saw as private) is definitely a good idea.
Summary
Be upfront about what you will see, how you will deal with that and what you'd rather not see before touching the device. Don't be pushy about your opinion on privacy. This is counterproductive as people will nearly always ignore it. You can try to steer the conversation in the right direction, but don't enforce anything. They may actually keep their history on purpose or don't care. In either way, it's their device and their way and you should refrain from questioning that, unless they put themselves in some serious risk (really serious, not "some dude might actually see that video you watched last evening").