In fact, both in Portugal and Brazil the pronunciation of s at the end of a syllable depends, even if you don’t realise it, on what comes after it: it is commonly pronounced /ʃ/ (as shape or ch in the Portuguese word chato) in Portugal and Rio and /sss/ (as in samba) elsewhere in Brazil if followed by an unvoiced consonant such as c, f, p, t or by a long pause; and it is pronounced /jƷ/ (as s in decision or j in the Portuguese word jarro) in Portugal and Rio and as /z/ (as in zoo) elsewhere in Brazil if followed by a voiced consonant such as b, d, g, m, or v. Of course you don’t have to worry about this if you’re only interested in pronouncing correctly: you vocal system will correctly choose between /ʃ/ and /jƷ/ and between /sss/ and /z/ without you even thinking about it. So (only /ʃ/,boldface /jʃ/, /ijƷ/, /sss/ and /zz/ are intendend to show actual pronunciation; the rest of the word is just as it is normally written):
EsbarrarEsbarrar is pronounced jbarrareƷbarrar in Portugal and Rio or izbarrarezbarrar elsewhere in Brazil.
MesmoMesmo is mejmomeƷmo or mezmomezmo.
EsvairEsvair is jvaireƷvair or izvairezvair.
CascoCasco is caʃcocaʃco in Portugal & Rio and casscocasco elsewhere in Brazil.
CastaCasta is casʃtacaʃta or casstacasta.
At the end of a word the sound depends on the first sound of the next word unless there is a long pause, in which case it is /ʃ/ or /sss/. If the next word starts with a vowel the sound is /z/ in both Portugal and Brazil.
Bons diasBons dias sounds bonj diaʃ inbonƷ diaʃ in Portugal & Rio or bonz diass elesewherebonz dias elesewhere in Brazil.
As portas abertasAs portas abertas sounds aʃ portaz abertaʃ oraʃ portaz abertaʃ or ass portaz abertass.as portaz abertas.
The reason for this is that the mouth takes the same position to pronounce /ʃ/ or /jƷ/, the only difference being that the throat vibrates to produce /jƷ/ (that is why this sound is called voiced) and does not vibrate to produce /ʃ/ (that is with it is unvoiced). The same is true of the voiced /z/ and unvoiced /sss/. You can check this by placing your finger tips on your throat (see this answer). And the throat cannot stop or start vibrating fast enough to smoothly pronounce one voiced consonant followed by an unvoiced one or vice-versa. So we pronounce the s as voiced (/jƷ/ or /z/) if the following consonant is voiced; and as unvoiced (/ʃ/ or /sss/) if the following consonant is unvoiced