Simplest answer: Tell the players you're doing this.
Don't tell, show (well,
do tell, but also show):
1. Have enemies flee or surrender, a LOT. Only a few kinds monsters should fight to the death (undead, constructs, summoned creatures); for the rest, they should stop fighting when they are reduced to half strength or so. You can make a formal morale rule for this if you want, but as a DM I usually just eyeball it, and that works fine.
Important: give full XP for enemies that flee or surrender (if you're doing monster XP). Losing XP is what players fear most about allowing monsters to get away.
2. Have monsters demand the PCs surrender, and have them treat the captured PCs well. You should do this a few times early-on in the adventure when the PCs are easy to scare. Half-way through the goblin encounter, a dozen hobgoblins show up, with some ogres! After they capture the PCs, they take all their gold and jewels and food, but then turn them loose with all their equipment.
Important: don't have enemies take the PC's magic items, because this is what players fear most about surrender. It's worse than losing gold or even losing levels because magic items are unique and "irreplaceable." Story-wise, the monster group that's powerful enough to defeat the PCs probably has no need for their paltry magic items. (You can make an exception if the PCs have a plot item that everyone is after, but you should telegraph that. Also, it can be fun to have your gear stolen, and then kill the enemies in revenge and take it back, but be careful with this; it's easy to overdo it.)
3. Have some enemies call for parley before combat starts. The parties size each other up and the one that thinks its stronger calls for tribute from the one that's weaker. The classic "we are bandits charging a reasonable toll" works well for this. The side that receives the tribute should also offer some kind of minor favor in return, to make it seem fair. "Pay us and we won't kill you, plus we'll tell you how to get past the room with the fire trap" is a more palatable deal for the players.
Important: Let the players know whether the other side in the parley can be trusted to keep their end of the bargain. Tell them explicitly. Because if the other side betrays the deal, the players will be discouraged from making future deals. But, not every NPC can be trusted, so call for Wisdom (Insight) checks. If the players decide how to deal with the untrustworthy NPCs, the parley remains a fun encounter (but of a somewhat different kind, and possibly erupting into combat).
4. Put the PCs into conflict with NPCs that they are better off not killing. The City Guard is a good example. Another might be an NPC who knows an important clue, or whose cooperation makes things a lot easier. Or have an NPC that is an enemy to some PCs and an ally to others. This is the most tricky of my suggestions because it's really difficult to predict what kinds of things will motivate the players to keep "enemy" NPCs alive.
Important: Plan for the NPC to die anyway. Because the players might not realize the benefit of keeping them alive, or may just be in the mood to slaughter somebody. So don't encourage the PCs into combat with a plot-essential NPC. (Really, don't have plot-essential NPCs.)
None of this will work if your players are dedicated murder-hobos. But if they're willing to try this play-style, moves like this can help them get into character.