The behavior you mention often occurs in spring and early summer. This is the time of year when most birds establish their territories, find a mate, lay eggs, and raise young. To ensure success, they defend their territory aggressively, and will attack and try to drive away any bird they view as a possible competitor or a threat to their young. When they see their own reflection in your window, they assume they’re seeing a competitor and attack the image. The species most likely to do this are those that nest close to houses, such as American Robins, Northern Cardinals, bluebirds, California Towhees, Chipping Sparrows, and Song Sparrows. Both males and females engage in this.
Fortunately, this behavior usually dissipates within a few days or, at most, weeks. But while it lasts, the bird may exhaust or even hurt itself, and it distracts the bird from far more important activities. And this behavior can be extremely annoying for the people witnessing it.
To get rid of the reflection, you must alter the outside of the window. You can cover it with netting, fabric, or newspaper, or smear soap streaks on the glass. When you’re no longer seeing the bird nearby you can remove this. Often, rubber snakes frighten birds away, at least temporarily—although like any object that doesn’t move, the birds get used to seeing them.
We have more information about protecting your birds from windows here.