Mark 5:1 They went across the lake to the region of the Gerasenes.
11 A large herd of pigs was feeding on the nearby hillside.
Gerasenes was a gentile region where people raised pigs. They were not the over-zealous Jews who wanted to make him an earthly king. In fact, they wanted Jesus to leave them alone.
Mark 5:17 And they began to implore Him to leave their region.
In this case, Jesus wanted the gentile people to know the good news of His power to deliver.
After this, he traveled to a crowded Jewish region.
Mark 5:21 When Jesus had again crossed over by boat to the other side of the lake, a large crowd gathered around him while he was by the lake. 22Then one of the synagogue leaders, named Jairus, came, and when he saw Jesus, he fell at his feet.
After he put them all out, he took the child’s father and mother and the disciples who were with him, and went in where the child was. 41He took her by the hand and said to her, “Talitha koum!” (which means “Little girl, I say to you, get up!”). 42Immediately the girl stood up and began to walk around (she was twelve years old). At this they were completely astonished. 43He gave strict orders not to let anyone know about this, and told them to give her something to eat.
Jesus didn't want the riotous crowd to make him king.
John 6:15 Jesus, knowing that they intended to come and make him king by force, withdrew again to a mountain by himself.
So Jesus didn't mind the non-Jews to talk about his miracles but he minded the over-zealous Jews making him an earthly king.
This is confirmed in the story of the Samaritan woman at the well also.
John 4:39 Many of the Samaritans from that town believed in him because of the woman’s testimony, “He told me everything I ever did.”
Samaritans and Gentiles talking about Jesus's power was okay but he didn't want the over-zealous Jews to do the same because they misunderstood the purpose of His miracles.