Any blood from the penis should be investigated as a matter of urgency.
Don't wait until this may happen again. If you have not already done so, make an urgent appointment to see your doctor for an urgent referral to a urologist.
Follow the advice that Dalubaba gave you to calm you and any sexual urges.
Let us know how you get on.
Happened to me years ago except when I peed and noticed a lot of blood. Go to the Dr ASAP. Mine turned out to be a tumor in my bladder but the Dr. got it all out and I've been clear for 2 years now but for a man seeing blood come out there is the one thing you never want to see. Please take care of yourself.
Stop masturbating till you see your doctor. If there is a severe sexual urge, do deep and slow abdominal breathing. Google for abdominal breathing. Learn and practice it. Do it when your sexually aroused. This breath will calm your urge. Do it till you consult your doctor.