Definition of Sack
The league defines a sack as tackling the quarterback behind the line of scrimmage while he is attempting to pass. All three elements must be present. It has to be the quarterback, not another player taking a handoff or pitch and throwing an option pass. It has to be behind the line of scrimmage; a quarterback who is chased across the line is credited with run yardage. And the quarterback must be trying to pass; designed run plays that are stopped behind the line of scrimmage are not considered sacks.
(if the intent of the QB is unclear it would be counted as a sack as well).