There are a number of risks to doing work with the team or doing the product owner's work as the scrum master. Before getting into those, let's be honest, it's a balance you have to find in your particular circumstance. If a major deal is going to be lost and you can help by stepping in and doing some testing or coding, that probably is the way to go. So, that said, the risks:
Doing PO Work
Writing stories, talking with stakeholders, or keeping a healthy backlog are things the PO should be doing. If they aren't and you fill in for them, you're enabling the problem. Maybe it's a problem with the PO or maybe the organization is making unreasonable demands on their time. Either way, you are helping hide the problem. Please note this is different than sitting down and helping the PO do these things when they are new to it and need a hand.
Doing Team Work
There are two possible risks here. First and simplest, by stepping in and doing work you are changing the view on what the team can deliver. I assume this is the exception and that means that 1) the extra capacity for work isn't sustainable and 2) you're doing work that the team will be held to later without the responsibility of you being part of the delivery team.
The other concern is if you are doing work because you have a skill they lack. Here, you are again allowing the team to cover a problem up that will persist. Like I said at the beginning, you have to weigh the risk. Maybe you are filling in for one sprint until a new hire starts and provides that skill. Just be aware of the risk you are adding in.
Healthy Tension Between Roles
There is one final thing to consider. The Scrum Team is designed with a healthy tension between roles. The Product Owner wants the team to spend their time building more and better features. The Team wants to spend time making sure the product is built right from a technical and architectural standpoint. The Scrum Master wants everyone to spend time improving how they work. When these three are in balance, you get all of the benefits. By investing yourself in either development work or PO work, you throw off this balance because now you are no longer an impartial coach. You have a personal investment in some part of the product.